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 have a set of tests that use TypeMock and a set that dont, all are run together in one NUnit session. I want to ensure that if I run the tests that use TypeMock first then they dont affect the other ones.

The problem is that I'm using the MockAll functionality and cant find a way to switch it off once a test is done, is there any way to do this?

Thanks,

Colin
asked by colinjack (1.2k points)

7 Answers

0 votes
Hi Colin,
I'm using the MockAll functionality and cant find a way to switch it off once a test is done, is there any way to do this?

There are a few ways to stop mocking (Sorted in preferred way first):
:arrow: MockManager.Verify()
This will Clear the mocks as well, although it might lead to test faliures if expected calls from other tests where not called. This should actually be called after each test in the [TearDown] to avoid tests accidently using another tests' expectations
:arrow: MockManager.Init()
This will clear all the expectations
:arrow: theMock.Clear()
This clears the expectations of the mock
:arrow: MockManager.GetMockAll(typeof(MyType)).Clear()
This does the same as above but you can use this if you don't have access to the mock variable (You can use IsTypeMockedAll to check if the type is mocked at all)
:arrow: theMock.Clear("TheMethod")
This clears the expectation of "TheMethod" method of the mock

:idea: Using the Enterprise Edition you can set some expectations in a 'Block' and clear just that block, this way you can have some test that use all the expectations and others use only part of the expectations

Hope this helps
answered by scott (32k points)
0 votes
Hello,

I'd been using MockManager.Verify to try and clear them after each test (and when I got desperate in the TestFixtureTearDown method). This didnt work but when I switched to using MockManager.Init my problem was resolved.

Thanks,

Colin
answered by colinjack (1.2k points)
0 votes
Hi,
Great to hear that you solved the problem.
I will check to see if this is a bug in the Verify method.

In any case, this might happen if the Verify is in the actual test method (as opposed to the [TearDown]) and the test fails before the Verify is called.
answered by scott (32k points)
0 votes
In previous verisons of TypeMock, I would call myMock.Verify() and it seemed like subsequent tests were independent of any previous mocks. I have recently installed 3.0 and this does not seem to be working now. I am getting this error

System.ArgumentException : Item has already been added. Key in dictionary: "TypeMock.e" Key being added: "TypeMock.e"

Is it better to just use MockManager.Verify in Teardown instead of explicity calling verify on the mock objects by themselves?
answered by jnapier (9.6k points)
0 votes
Hi,
What you are describing could be a bug. :twisted:
Please give an example where you get System.ArgumentException.

In any case it is best to call MockManager.Verify in the TearDown.
This is because your test might have failed before the mock has been verified and this will lead to dependancies between tests.
answered by scott (32k points)
0 votes
This started occurring after the patch you sent me for another fix. Here is some code for one test. this test runs after another test that almost does the same thing. The error occurs as soon as a new Member object is instantiated if Member has already been mocked in another test. So in the following code, the exception will occur when helper.CreateMember is called.

         
         Mock memberMock = MockManager.Mock(typeof(Member));
         memberMock.ExpectGet("HasActiveJobPost", false, 1);

         //create a new member
         DomainModelHelper helper = new DomainModelHelper();
         Member user = helper.CreateMember(new Role(Role.BenefitsUser, Role.BenefitsUser));

         //create the context for the test procedure with our user
         OperationManagementPolicy policy = new OperationManagementPolicy(user);
         IOperationContext context = policy.GetContext(this.TestProcedure);

         //have the authorization service return false when the approve permission is checked
         this.MockAuthorizationService.ExpectAndReturn("Authorize", false, 1).Args(policy.Principal, DomainPermission.Operation.Approve);
         
         //verify that an operation can not be approved
         Assert.IsFalse(context.IsPermitted(OperationAction.Approve));

         //veriy that the mocks were called
         this.MockAuthorizationService.Verify();
         memberMock.Verify();
answered by jnapier (9.6k points)
0 votes
Hi,
This is solved in version 3.0.1 8)
answered by scott (32k points)
...