7

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()?

urig
  • 16,016
  • 26
  • 115
  • 184
  • 1
    You haven't included a full snippet, so I'll ask the obvious question... Have the tests/class been marked with the `Isolated` attribute, or are you calling `Isolator.Cleanup` to reset the behaviour? http://www.typemock.com/rule-missing-isolated-attribut – forsvarir Jun 23 '15 at 20:57
  • @forsvarir Thank you. Adding the `[Isolated]` attribute fixed the issue. Would you like to post this as an answer so I can accept it? – urig Jun 24 '15 at 06:15

1 Answers1

6

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.

forsvarir
  • 10,749
  • 6
  • 46
  • 77