【问题标题】:Mocking same interface more than once in Rhino在 Rhino 中多次模拟同一个界面
【发布时间】:2014-10-02 10:45:57
【问题描述】:

我想多次模拟同一个界面。下面的代码演示了我认为阻止我的原因。

我已经阅读并找不到解决方案。似乎没有办法区分 _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
    }
}

如果我调试代码,我发现这两个对象具有相同的标识符。

我意识到这可能更多地与糟糕的抽象有关,但如果有办法解决这个问题,这将对我们项目的当前位置非常有帮助。

非常感谢。

【问题讨论】:

标签: c# testing mocking rhino-mocks


【解决方案1】:

事实证明,应用程序的复杂性让我偏离了方向。

我实际上是一个平等问题。我期待的参数是复杂的对象,实际上失败了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);

完成。

【讨论】:

  • 如果有人能回答为什么重写 Equals()GetHashCode() 方法没有帮助,我想听听为什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2011-01-19
  • 2022-01-06
相关资源
最近更新 更多