Search code examples
c#unit-testingisolationtypemocktypemock-isolator

TypeMock Isolator: WillThrow() bleeds across unit test boundaries?


I have two unit tests that use TypeMock Isolator to isolate and fake a method from asp.net's SqlMembershipProvider.

In test 1 I have:

        Isolate.WhenCalled(
            () =>
                Membership.CreateUser(...)))
            .WithExactArguments()
            .WillThrow(new Exception());

In test 2 I have:

        Isolate.WhenCalled(
            () =>
                Membership.CreateUser(...)))
            .WithExactArguments()
            .WillReturn(new MembershipUser(...));

When I run each test by itself they both pass successfully.

When I run both tests, test number 1 runs first and passes, then test number 2 runs and fails with the exception thrown in test 1.

Why would the WillThrow() instruction in test 1 "bleed over" to test 2? After all, test 2 explicitly defines different behavior - WillReturn()?


Solution

  • If TypeMock behaviours are bleeding between tests, then the first thing to check is that you're cleaning up between tests. You can do this explicitly by calling Isolater.CleanUp(), or using the preferred approach which is to decorate either the test methods or the test class itself with the [Isolated] attribute.