I have used the following successfully to reset expectations between tests:
MyMock.BackToRecord(BackToRecordOptions.Expectations);
MyMock.Replay();
However, in the case where the second test tries to throw an exception on the mocked method, this doesn't seem to work:
[SetUp]
public void Setup()
{
//reset expectations for each test
MockRestQuestionClient.BackToRecord(BackToRecordOptions.Expectations);
MockRestQuestionClient.Replay();
}
[Test]
public void Test1() {
MyMock.Expect(t => t.Something()).Returns(someObject);
}
[Test]
public void Test2() {
//this works when run individually, but fails if run after Test1
MyMock.Expect(t => t.Something()).Throw(new Exception());
}
Any ideas what I'm doing wrong?
Thanks!
It seems you're using the same mocks for different test cases.
Do you have any specific reason to share mocks between tests?
I assume your issue should disappear if you instantiate new mock objects per each test (in Setup()
method.
UPDATE
Please read good comment for sharing data between tests here.
UPDATE2
If you use base class to set up mocks then I'd suggest to have particular method in base class which does set up (e.g. Init()
). This method should be marked with attribute [SetUp]
.
As result Init()
will be run before each test. And fresh mocks will be used in each test. So you won't need to reset existing mocks.