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\'ve been using Typemock for less than a day so be gentle.

I\'ve got a group of tests that all pass when ran individually, but fails a few when I run them all (via Unit Test Runner in VS2003).

Looking at the trace for the first failed test, I see there is an Instance 1 and an Instance -1. What should this tell me?

Here\'s some of the code leading up to the first failed test:

private Mock GetMockOneCardServiceConnected()
{
Mock serviceMock = MockManager.MockAll(typeof (OneCardService));
ServiceResult verifyResult = new ServiceResult();
verifyResult.Success = true;
verifyResult.Authenticated = true;
serviceMock.ExpectAndReturn(\"VerifyService\", verifyResult);

return serviceMock;
}

private BridgeServiceResult GetBridgeServiceResultSucess()
{
BridgeServiceResult serviceResult = new BridgeServiceResult();
serviceResult.Success = true;
serviceResult.Authenticated = true;

return serviceResult;
}

private IOneCardConnection GetConnection()
{
IOneCardConnection conn = new OneCardConnection(\"userName\", \"password\", \"http://localhost\");
System.Threading.Thread.Sleep(2000); //wait for the timer to fire so we get connected to the OPR
return conn;
}

[Test]
public void TestAddRedemptionNoError()
{
Mock serviceMock = GetMockOneCardServiceConnected();
serviceMock.ExpectAndReturn(\"RedemptionAdd\", GetBridgeServiceResultSucess());

IOneCardConnection connection = GetConnection();
BridgeServiceResult result = connection.AddRedemption(1, -1, \'D\', 1, 1);
Assert.IsTrue(result.Success, \"AddRedemption not successfull\");

MockManager.Verify();
}

All help appreciated.

On a slightly different note... How do verify that a specific method isn\'t called? Something like Expect.Never(...)

Thanks.
Scott C.
asked by (2.4k points)
edited by

3 Answers

0 votes

Hi Scott,

Welcome to Typemock.
Here are some tips that can help:
1. When single tests run but a group doesn\'t it is a sign that the tests are not isolated and don\'t clean up after themselves.
2. When you have a -1 instance, it is a sign that an object was created before the mock was kicked in.

Both of these together can point to the problem, which can be one of the following.
1. You forgot to clean up expectations after the tests.
:idea: use MockManager.CleanUp(); in you [TestTearDown] method.

2. You have mocked an object and created it in one test and then use the same object in another test (a singleton for example).
:idea: To solve this, either make sure that the mocks are kept alive through the tests (you can do this by verifying the mocks manually (not using MockManager.Verify()), and making sure that the other mocks are not verified until [TestFixtureTearDown]).
Or use the VerifyMode.DontClear Enterprise Feature (see Advanced Stubs).

To Verify that a call has never been made use mock.Strict = true or method specific strict (see Arbitary Calls).
Perhaps we should have a mock.NoMoreCalls(\"method\");

answered by (32k points)
edited by
0 votes
Thanks for the informative reply, but I do have a couple more questions.

I'm using MockManager.Verify() and MockManager.ClearAll() (in my TearDown) and I don't have a MockManager.CleanUp(). Is this a version thing? I'm using 3.5.1.0.

My expectation is that all objects (mocked or not) will be freed after each test and each test creates all of the objects it needs. I'm not sure how best to do this. My tests do use a lot of the same types of objects, but I'm creating new objects for each test (see code in original post). Should I switch from MockAll to Mock to help isolate my test cases?

Thanks again for the quick response.
Scott C.
answered by (2.4k points)
0 votes
FWIW, I changed the variables holding my mocks to have unique names in each test and it seems to have fixed the problem. Something like this:

[Test]
public void Test1()
{
Mock mock1 = CreateMock();
//do stuff and verify
}

[Test]
public void Test2()
{
Mock mock2 = CreateMock();
//do stuff and verify
}

Thanks again.
Scott C.
answered by (2.4k points)
...