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 Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!
0 votes
I have a set of tests in a test class, 2 of which contain a call to a method that accesses a database. The first test mocks the method call. The second test does not mock the method call ( I really want to access the database with this test).

I set up and verify my mock objects per test (i.e. not in the [SetUp ] method)

It is the second test that I am interested in here. If I run all the tests (i.e., in Nunit I select the test class node), the non-mocked method throws a TypeMock.VerifyException. If I select the individual test and run it, the non-mocked class executes as expected.

Is there a way to switch between mocked and non-mocked code for a method call?

I've tried
      this._IsMockingOn=false;

         if (this._IsMockingOn)
         {

            MockManager.Init();
            Mock MockMyObject = MockManager.Mock(typeof(MyObject));
            MockMyObject.ExpectAndReturn("MyMethod",MyReturn).Args(MyArg);
         }
         //Run tests

         if(this._IsMockingOn)
         {
            MockManager.Verify();
         }


This code appears in other tests with
this._IsMockingOn=true
;


I am using Nunit and TestDrive.NET.

TIA
asked by (1.1k points)

3 Answers

0 votes
It is the second test that I am interested in here. If I run all the tests (i.e., in Nunit I select the test class node), the non-mocked method throws a TypeMock.VerifyException. If I select the individual test and run it, the non-mocked class executes as expected.


Hi TIA,
It seems that your tests are not independant: That running the tests individually and in a group don't act the same. This could happen if you forgot to verify the mocks in a previous test.
In order to perform the same test once mocked and the other not mocked; I think that the best pattern would be to extract the duplicate test code and to call them from each test as follows:

[Test] public void WithMocks() 
{ 
   MockManager.Init();
   SetupMocks();
   // run tests
   RunXXX();
   MockManager.Verify();
}

[Test] public void WithoutMocks() 
{ 
   SetupDatabase();
   // run tests
   RunXXX();
   // Check
   Assert...
}

I hope this helps.

BTW. In the next version, that will be out soon, you will be able to use: MockManager.IsInitialized to check if mocking is on.
answered by (3.9k points)
0 votes
Thanks for your reply. You were right in your assumption that a verify was not being called. When I re-examined my code I realised that I was testing for an exception (not from the mocked object) and the exception was being thrown before the verify method was being called. The test wasn't failing because I was testing for an exception (which was happening). :?

Oh well, we live and learn :wink:

Thanks for taking the time to look into my problem. It's nice to know that we will get the chance to test for IsInitialised. Keep up the great work. TypeMock is a fantastic tool, by far the easiest .Net mocking framework I've used.
answered by (1.1k points)
0 votes
Thanks for taking the time to look into my problem. It's nice to know that we will get the chance to test for IsInitialised. Keep up the great work. TypeMock is a fantastic tool, by far the easiest .Net mocking framework I've used.


I am glad to know that I have solved your problem. Thanks for the compliment. We are trying very hard to create and support this framework, so I am happy that you find it usefull. :D

:idea: BTW. You can always do a MockManager.Init(); in the [setup] method, as this will clear all your previous expectations. You can also use Clear() to clear the expectations of a single mock (This is very usefull when creating a mock framework).
answered by (3.9k points)
...