chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

I am using winForms, VS2015 and typemock isolator 8.6.0.0. I was trying to mock out a call to a property setter but I only see the getter being mocked. Is there a way to intercept the call to the setter? (a way to control calls to the setter, not just the example here where trying to throw the exception)

Here is a simple class:

 public class PhotonTorpedo
 {
        public int EnergyLevel
        {
            get { return 5; }
 
            set
            {
                Debug.WriteLine("Set");
            }
        } 
 
        public PhotonTorpedo(){}
}
 
I would like to fake this class, and when the setter is called, throw an exception. But I don't see how to do this. Sample test:
 
 
[TestMethod]
public void SetterTest()
{
    var fakeTorpedo = Isolate.Fake.AllInstances<PhotonTorpedo>();
 
    Isolate.WhenCalled(() => fakeTorpedo.EnergyLevel).WillThrow(new Exception(""));
    string msg = "";
 
    var testTorpeo = new PhotonTorpedo();
    try
    {
        testTorpeo.EnergyLevel = 100;
    }
    catch (Exception ex)
    {
        msg = ex.Message;
    }
 
    Assert.AreNotEqual(msg, "");
}
 
asked by meansala (3.6k points)
edited by meansala

Please log in or register to answer this question.

...