2

我们有一个测试场景,其中我们有一个基类,它为每个测试(在 [SetUp] 方法中)更新所有可注入依赖项的实例。但是,无论我做什么,在一个测试中设置的存根或期望值都将保留在后续测试中。只需运行以下两次,我就看到了,当我打电话时

_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
     }
}
4

0 回答 0