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
Howdy, I am wondering if there is any shorthand for mocking an object with Members.MustSpecifyReturnValues, but still have a property with getter/setter act as an autoproperty and not call the underlying implementation.

For instance:

   public class TestMe
   {
      public MockMe Mock;

      public TestMe(MockMe mock) { Mock = mock; }

      public void PrintMockA()
      {
         Console.WriteLine(Mock.A);
      }
   }

   public class MockMe
   {
      public int A { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }

      public void PrintA()
      {
         Console.WriteLine(A);
      }
   }

   [Test]
   public void Works()
   {
      var mockMe = Isolate.Fake.Instance<MockMe>(Members.MustSpecifyReturnValues);

      // I want to achieve this without writing such verbose code
      int mockMe_a = 0;
      Isolate.WhenCalled(() => mockMe.A).DoInstead(context => mockMe_a);
      Isolate.WhenCalled(() => mockMe.A = 0).DoInstead(context => mockMe_a = (int) context.Parameters[0]);

      mockMe.A = 42;
      Console.WriteLine(mockMe.A);
      mockMe.PrintA();

      var testMe = new TestMe(mockMe);
      testMe.PrintMockA();
   }
   
   [Test]
   public void DontWork()
   {
      var mockMe = Isolate.Fake.Instance<MockMe>(Members.MustSpecifyReturnValues);
      mockMe.A = 42;
      Console.WriteLine(mockMe.A);
      mockMe.PrintA();

      var testMe = new TestMe(mockMe);
      // throws NullReferenceException
      testMe.PrintMockA();
   }


I basically want to achieve the functionality of Works() without having to write such verbose code (I'm lazy, and also there are a lot of these properties).

Strangely, in DontWork(), mockMe.A does act as an auto-property... except where I most want it to, which is when it's used within the class under testing (as shown). I don't really understand why it throws a NullReferenceException.
asked by adante (1.7k points)

1 Answer

0 votes
Hi

Currently it is not you can't do that because it contradicts the default behavior you specified when you created the fake object.
However it might a good idea for a feature, I'll add your idea to our feature requests.
From the second test it seems like we have a bug since the behavior of the Isolator is not consistent.
answered by ohad (35.4k points)
...