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
I can't understand VerifyMocks attribute behavior.
[TestCleanup]
class clA
{
   private static int a = 10;
   public static int A
   {
      get
      {
         return a;
      }
   }
}

[TestCleanup]
public void Cleanup()
{
   int controlA = clA.A;
}

[TestMethod]
public void TestMethod1()
{
   using (RecordExpectations record = new RecordExpectations())
   {
      record.ExpectAndReturn(clA.A, 5);
      record.RepeatAlways();
   }
   int a = clA.A;
}


When i get controlA it equal 5. If i write VerifyMocks attribute before TestMethod1:
[TestMethod, VerifyMocks]
public void TestMethod1()
{
   using (RecordExpectations record = new RecordExpectations())
   {
      record.ExpectAndReturn(clA.A, 5);
      record.RepeatAlways();
   }
   int a = clA.A;
}

controlA equals 10. Is it a correct behavior?
asked by Spi (600 points)

1 Answer

0 votes
Hi
The problem here is that VerifyMocks cleans up the mocks at the end of the test method and before your Cleanup method gets to run.

Note that is usually recommended to do the asserts at the end of each test
and not the cleanup methods.
Take a look at the Arrange Act Assert API
I think it make such things clearer.

Just an example of the same test with an Arrange Act Assert syntax:
[TestMethod, Isolated]
public void TestMethod_AAA_Syntax()
{
    // Arrange
    Isolate.WhenCalled(()=>clA.A).WillReturn(5);

    // Act
    int result = clA.A;

    // Assert
    Assert.AreEqual(5, clA.A);
}
answered by ohad (35.4k points)
...