[英]Ninject 2.0 - binding to a object that uses the same interface more than once?
[英]Mocking same interface more than once in Rhino
我想多次模擬相同的界面。 下面的代碼演示了我認為阻止了我的事情。
我已經閱讀了一遍,找不到解決方案。 似乎無法區分_mockOne和_mockTwo。
public interface IDoSomething
{
}
[TestFixture]
class RhinoTest
{
private IDoSomething _mockOne;
private IDoSomething _mockTwo;
[SetUp]
public void SetUp()
{
_mockOne = MockRepository.GenerateMock<IDoSomething>();
_mockTwo = MockRepository.GenerateMock<IDoSomething>();
var somethings = new Dictionary<string, IDoSomething>
{
{"one", _mockOne},
{"two", _mockTwo}
};
//Pass this dictionary to a constructor for use in tests
}
}
如果調試代碼,則會發現兩個對象具有相同的標識符。
我意識到這可能與不良抽象有關,但如果有解決辦法,它將對我們項目的當前位置確實有幫助。
提前謝謝了。
原來,我被應用程序的復雜性弄得一團糟。
我實際上是一個平等問題。 我期望的參數是復雜的對象,實際上Assert.AreEqual(...);
失敗Assert.AreEqual(...);
希望在這種情況下加快其他人的速度。 這是一個失敗的測試。
[Test]
public void Test()
{
var expectedInput = new Object1 { DeeperObject = new Object2 { MyString = "Hello World" } };
const string expectedOutput = "Hello Matt!";
_mockOne.Expect(s => s.ReturnSomething(expectedInput))
.Return(expectedOutput);
var actualInput = new Object1 { DeeperObject = new Object2 { MyString = "Hello World" } };
var actualOutput = _mockOne.ReturnSomething(actualInput);
Assert.NotNull(actualOutput);
}
要解決此問題,需要特別匹配參數屬性。
_mockOne.Expect
開頭的行替換為:
_mockOne.Expect(s => s .ReturnSomething(Arg<Object1> .Matches(o => o.DeeperObject.MyString == expectedInput.DeeperObject.MyString))) .Return(expectedOutput);
做完了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.