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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi, I found that having property in recording block can cause unexpected problem..

Here's the code:

    public class Dummy
    {
        public Dummy()
        {

        }

        public string MyDumn
        {
            get;
            set;
        }
    }


Here's the test code:

    [TestFixture]
    [ClearMocks]
public class TestClass
{
        private string DataDirectory
        {
            get
            {
                string filePath = "Wai test";;
            }
        }

        [Test]
        [VerifyMocks]
        public void TestProperty()
        {
            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                Dummy dum = new Dummy();
                recorder.ExpectAndReturn(dum.MyDumn, DataDirectory);
            }
            Dummy dum1 = new Dummy();
            Assert.AreEqual(DataDirectory, dum1.MyDumn);
        }

        [Test]
        [VerifyMocks]
        public void TestProperty2()
        {
            string dumData = DataDirectory;
            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                Dummy dum = new Dummy();
                recorder.ExpectAndReturn(dum.MyDumn, dumData);
            }
            Dummy dum1 = new Dummy();
            Assert.AreEqual(DataDirectory, dum1.MyDumn);
        }
}



TestProperty passes whereas TestProperty2 failes. It seems that if I invoke a property directly in recording block then I will have a TypeMockException
TestCase 'XFootingTest.PresenterTest.TestProperty'
failed: TypeMock.TypeMockException : 
*** Cannot return a value for Dummy.get_MyDumn() because no value was set. use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly.
   at f.c()
   at TypeMock.RecordExpectations.Dispose()
   C:developmentEsteem7 .Net2XFootingXFootingTestPresenterTest.cs(92,0): at XFootingTest.PresenterTest.TestProperty()
   at TypeMock.VerifyMocksAttribute.Execute()
   at TypeMock.DecoratorAttribute.CallDecoratedMethod()
   at TypeMock.ClearMocksAttribute.Execute()
   at TypeMock.MethodDecorator.e()
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)


Is this expected or by design?
________
buy grinder
asked by nsoonhui (59.1k points)

2 Answers

0 votes
Not sure if that's intended behavior or not, but if you watch the problem test execute in the tracer, you can see that there's an expectation getting set up around the TestClass.DataDirectory property, probably in that ExpectAndReturn line. I validated that in Isolator 4.3.1.

In general, I try not to execute any code in a recorder block that I don't actually intend to mock. Even if it's the return value in an ExpectAndReturn, I always figure, "If I put it in a recorder block, it's going to get mocked."

If you need to get your expected value out of a property, get it before the recorder block and hold a local reference to it, like this:

[Test] 
[VerifyMocks] 
public void TestProperty()
{
  string val = DataDirectory;
  using (RecordExpectations recorder = RecorderManager.StartRecording())
  {
    Dummy dum = new Dummy();
    recorder.ExpectAndReturn(dum.MyDumn, val);
  }
  Dummy dum1 = new Dummy();
  Assert.AreEqual(DataDirectory, dum1.MyDumn);
}


Removing the call to DataDirectory in the ExpectAndReturn method fixes the test and still accomplishes the same thing.
answered by tillig (6.7k points)
0 votes
This is one of the reasons we created the AAA syntax.

In your case it goes like this:
var fakeDummy = Isolate.Fake.Instance<Dummy>();
Isolate.SwapNextInstance<Dummy>.With(fakeDummy);

Isolate.WhenCalled(()=> fakeDummy.MyDumn).WillReturn(DataDirectory);

Dummy dum1 = new Dummy();
Assert.AreEqual(DataDirectory, dum1.MyDumn);
answered by eli (5.7k points)
...