Rhino Mocks - AssertWasCalled:如何在参数不正确时改进不清楚的诊断消息

发布于 2024-10-10 05:39:53 字数 1038 浏览 4 评论 0原文

恕我直言,当使用 AssertWasCalled 来验证是否已使用特定参数调用方法时,Rhino Mocks 会生成不清楚的诊断消息。

示例:

interface ISomeInterface
{
    void Write(string s);
}

[TestFixture]
public class SomeTests
{
    [Test]
    public void WriteShouldBeCalledWithCorrectArguments()
    {
        // Arrange
        var mock = MockRepository.GenerateMock<ISomeInterface>();
        var sut = new SomeClass(mock);

        // Act
        sut.DoSomething();

        // Assert
        mock.AssertWasCalled(x => x.Write(Arg<string>.Is.Equal("hello")));
    }
}

现在,如果测试失败并显示此消息...

Rhino.Mocks.Exceptions.ExpectationViolationException : ISomeInterface.Write(等于 hello);预期 #1,实际 #0。

...您无法知道它是否失败,因为

A.“写入”从未被调用 - 或 -
B. 实际上调用了“Write”,但参数不正确

如果 B 是失败的原因,那么如果消息读取如下内容,则会更加清晰:

Rhino.Mocks.Exceptions.ExpectationViolationException : ISomeInterface.Write(string arg): 方法被调用,但参数不正确:预期:你好,实际:再见

我可以自己修复这个缺点吗(通过以某种方式为Rhino编写自定义匹配器)或者我只是有为此编写手动模拟?

IMHO, Rhino Mocks produces an unclear diagnostic message when AssertWasCalled is used in order to verify that a method has been called with a specific argument.

Example:

interface ISomeInterface
{
    void Write(string s);
}

[TestFixture]
public class SomeTests
{
    [Test]
    public void WriteShouldBeCalledWithCorrectArguments()
    {
        // Arrange
        var mock = MockRepository.GenerateMock<ISomeInterface>();
        var sut = new SomeClass(mock);

        // Act
        sut.DoSomething();

        // Assert
        mock.AssertWasCalled(x => x.Write(Arg<string>.Is.Equal("hello")));
    }
}

Now, if the test fails with this message...

Rhino.Mocks.Exceptions.ExpectationViolationException : ISomeInterface.Write(equal to hello); Expected #1, Actual #0.

... you cannot know if it fails because

A. 'Write' is never invoked -or-
B. 'Write' is in fact invoked but with the incorrect argument

If B would be the cause of the failure then it would be so much clearer if the message would read something like this:

Rhino.Mocks.Exceptions.ExpectationViolationException : ISomeInterface.Write(string arg): Method was called but with the incorrect arguments: Expected: hello, Actual: bye

Can I fix this shortcoming myself (by writing custom matchers for Rhino in some way) or do I simply have to write a manual mock for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-10-17 05:39:54

我通过使用 Rhino 提供的“匹配”语法找到了一个简单的解决方案:

[Test]
public void WriteShouldBeCalledWithCorrectArguments()
{
    // Arrange
    var mock = MockRepository.GenerateMock<ISomeInterface>();
    var sut = new SomeClass(mock);

    // Act
    sut.DoSomething();

    // Assert
    mock.AssertWasCalled(x => x.Write(Arg<string>.Matches(s => Equal(s, "hello"))));
}

private static bool Equal(string s1, string s2)
{
    Assert.That(s1, Is.EqualTo(s2), "Unexpected argument");
    return true;
}

当然,它有点笨拙,但它可以完成工作。如果有更好的方法,请告诉我。

I've found a simple solution by using the "Matches" syntax provided by Rhino:

[Test]
public void WriteShouldBeCalledWithCorrectArguments()
{
    // Arrange
    var mock = MockRepository.GenerateMock<ISomeInterface>();
    var sut = new SomeClass(mock);

    // Act
    sut.DoSomething();

    // Assert
    mock.AssertWasCalled(x => x.Write(Arg<string>.Matches(s => Equal(s, "hello"))));
}

private static bool Equal(string s1, string s2)
{
    Assert.That(s1, Is.EqualTo(s2), "Unexpected argument");
    return true;
}

Sure, it's a little clumsy but it gets the job done. If there is a better way of doing it, please let me know.

池予 2024-10-17 05:39:54

您可以使用 GetArgumentsForCallsMadeOn 来获取传递的参数并断言它们:

var args = mock.GetArgumentsForCallsMadeOn(x => x.Write(null), opt => opt.IgnoreArguments())[0];
Assert.AreEqual("Hello", args[0]):

You could use GetArgumentsForCallsMadeOn to get passed arguments and assert them:

var args = mock.GetArgumentsForCallsMadeOn(x => x.Write(null), opt => opt.IgnoreArguments())[0];
Assert.AreEqual("Hello", args[0]):
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文