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
0 votes
Hi,
I have 3 tests in an NUnit test fixture. One test always fails if I run all tests. If I just run the test that fails, the test passes.

Each test uses the recorderManager and it tells the a mock what to return. I debugged the problem and it seems that the recorder did not get cleared and it returns the return value from the previous test.

How can I clear the recorder after each test? I tried the ClearMock attribute but it did not work.
asked by Step (3.4k points)

5 Answers

0 votes
Try using MockManager.Verify() or MockManager.ClearAll() in the test teardown method.
In any case, if you could post the code, we can help find out why the mocks where not cleared.
answered by scott (32k points)
0 votes
[TestFixture]
public class ServiceStartupManagerTests
{
[Test, Category("TypeMock"), ClearMocks, VerifyMocks]
public void TestStartServices()
{
ServiceStartupOrder ds = new ServiceStartupOrder();
ds.ServiceList.AddServiceListRow("Test0", 0, 0); //ignore
ds.ServiceList.AddServiceListRow("Test1", 1, 2);
ds.ServiceList.AddServiceListRow("Test2", 2, 1);

//Setup the test
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
// CAUTION: ALL calls here are mocked!!!
ServiceManagerHelper helper = new ServiceManagerHelper();
StartupValidator val = new StartupValidator();

recorder.ExpectAndReturn(helper.StartService("Test1"), true);
recorder.ExpectAndReturn(helper.StartService("Test2"), true);
//recorder.ExpectAndReturn(val.ReadyForStartup(Guid.Empty),true);
recorder.CheckArguments();
}

ServiceStartupManager mgr = new ServiceStartupManager();
mgr.StartServices(ds);

MockManager.Verify();
MockManager.ClearAll();
}

[Test, Category("TypeMock"), ClearMocks, VerifyMocks]
//Test fails right now, we need to figure out how to clear the recorder between each test
public void TestStartServices_NotReady()
{
ServiceStartupOrder ds = new ServiceStartupOrder();
ds.ServiceList.AddServiceListRow("Test0", 0, 0); //ignore
ds.ServiceList.AddServiceListRow("Test1", 1, 2);
ds.ServiceList.AddServiceListRow("Test2", 2, 1);

//Setup the test
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
// CAUTION: ALL calls here are mocked!!!
ServiceManagerHelper helper = new ServiceManagerHelper();
StartupValidator val = new StartupValidator();

recorder.ExpectAndReturn(val.ReadyForStartup(Guid.Empty), false);
recorder.CheckArguments();
}

ServiceStartupManager mgr = new ServiceStartupManager();
mgr.StartServices(ds);

MockManager.Verify();
MockManager.ClearAll();
}
[Test, Category("TypeMock"), ClearMocks, VerifyMocks]
public void TestStopServices()
{
ServiceStartupOrder ds = new ServiceStartupOrder();
ds.ServiceList.AddServiceListRow("Test0", 0, 0); //ignore
ds.ServiceList.AddServiceListRow("Test1", 1, 2);
ds.ServiceList.AddServiceListRow("Test2", 2, 1);

//Setup the test
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
// CAUTION: ALL calls here are mocked!!!
ServiceManagerHelper helper = new ServiceManagerHelper();

recorder.ExpectAndReturn(helper.StopService("Test2"), true);
recorder.ExpectAndReturn(helper.StopService("Test1"), true);
recorder.CheckArguments();

}

ServiceStartupManager mgr = new ServiceStartupManager();
mgr.StopServices(ds);

MockManager.ClearAll();
MockManager.Verify();

}
}

the test TestStartServices_NotReady fails. This is because the call recorder.ExpectAndReturn(val.ReadyForStartup(Guid.Empty), false) did not really return false, it returned true. I guess this is because of the prev test. I tried clearMock, same thing.

Thanks for the fast response
answered by Step (3.4k points)
0 votes
Hi,
You should use the Tracer.
I don't really know your code but the Validator should not be effected from other tests as there is a new in the recorder. This means that a new instance will be created in the production code and that will be mocked.
Perhaps the code doesn't create a new Validator or creates it only the first time.
answered by scott (32k points)
0 votes
Scott,
thank you very much. Yes the validator was created only once (Singleton). After changing that it worked fine.

How would I write a test if it is a singleton? I cannot tell when the new is called. How would you tell this the recorder?
answered by Step (3.4k points)
0 votes
You can use:
recorder.MockAllInstances = true; 
This will mock new and old instances.
answered by scott (32k points)
...