Hi,
I'm trying to replace an determined expectation:
mock.Expect(m => m.Do()) .Return("A");
//In some moment during my test execution I change the expectation for the mocked object
mock.Expect(m => m.Do())
.Return("B");
The problem:
The test infrastructure is too complex.
I don't know how many times the executing code will call the method "Do". The code I`m validating will run in a different thread. I don't care about how many times the method Do will be called, I'm only interested on the returning value.
I can't call BackToRecord, because there are other expectations that were already set for the mocked object and I can't lose them.
Possible solution:
I've decompiled the BacktoRecord:
public void BackToRecord(object obj, BackToRecordOptions options)
{
this.IsMockObjectFromThisRepository(obj);
if ((options & BackToRecordOptions.Expectations) == BackToRecordOptions.Expectations)
{
foreach (IExpectation allExpectationsForProxy in this.rootRecorder.GetAllExpectationsForProxy(obj))
{
this.rootRecorder.RemoveExpectation(allExpectationsForProxy);
}
this.rootRecorder.RemoveAllRepeatableExpectationsForProxy(obj);
}
MockRepository.GetMockedObject(obj).ClearState(options);
this.proxies[obj] = this.proxies[obj].BackToRecord();
foreach (IMockedObject dependentMock in MockRepository.GetMockedObject(obj).DependentMocks)
{
this.BackToRecord(dependentMock, options);
}
}
The call:
this.rootRecorder.RemoveExpectation(allExpectationsForProxy);
It is interesting: Can I remove only all the expectations for a single "mocked method" ?
I would like to be able to make something like this:
mock.Expect(m => m.Do()).Remove(); //or .Clear()
mock.Expect(m => m.Do()).Return("B");
Is there a way to make something like this?
(yes, I tried Repeat.Any, WhenCalled, Do, and a lot of other things. ps: It might be a threading problem as well).