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

1

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

Ecclesia answered 23/6, 2015 at 20:8 Comment(2)
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? typemock.com/rule-missing-isolated-attributJaquith
@Jaquith Thank you. Adding the [Isolated] attribute fixed the issue. Would you like to post this as an answer so I can accept it?Ecclesia
J
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.

Jaquith answered 24/6, 2015 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.