我们有一个测试场景,其中我们有一个基类,用于为每个测试(在[SetUp]方法中)新闻所有Injectible依赖项的实例。但是,无论我做什么,在一次测试中设置的存根或期望都会在后续测试中保留。只运行以下两次,我看到当我打电话
_someStub = MockRepository.GenerateMock<T>();
每次都返回相同的实例,即使我先将它设置为null,即使我像这样使用MockRepository的新实例:
repo = new MockRepository();
_someMock = repo.Mock<T>();
_someMock.Stub(t => t.SomeMethod()).Return(someObject);
我尝试过:
_someMock.BackToRecord();
_someMock.Replay();
_someMock.Stub(t => t.SomeMethod()).Return(someOtherObject);
但是对于第二个测试,SomeMethod总是返回someObject而不是someOtherObject。
我希望这是有道理的。简化了以下示例代码。
public MockTestBase {
protected ISomeClass _someMock;
[SetUp]
protected void DoSetup() {
_someMock = null; //just trying this, makes no difference
_someMock = MockRepository.GenerateStub<ISomeClass>(); //returns Castle.Proxies.ISomeClassxxxxxxxxxxx every time
}
}
public ControllerTest : MockTestBase {
protected MyController GetController() {
return new MyController(_someMock );
}
[Test]
public void TestOne() {
var object = new SomeObject() { SomeProperty = "1" };
_someMock.Stub(t => SomeMethod()).Return(object);
//execute test
}
[Test]
public void TestTwo() {
var object = new SomeObject() { SomeProperty = "2" };
_someMock.Stub(t => SomeMethod()).Return(object);
//execute test
//SomeMethod() returns object from TestOne
}
}