RhinoMocks/AssertWasCalled:验证参数长度?

发布于 2024-10-06 03:53:18 字数 302 浏览 2 评论 0原文

我的 MSpec 测试将断言使用(至少)给定长度的参数调用给定方法。

尽管参数(在运行时)的长度为 534,但此语法使断言失败:

_foo.AssertWasCalled(x => x.Write(Arg.Text.Like(".{512,}")));

ExpectationViolationException: IFoo.Write(如“.{512,}”);预期 #1,实际 #0。

我对 Like() 的模式做错了什么?

My MSpec test will assert that a given method was called with an argument of (at least) a given length.

This syntax is failing the assertion, despite the argument (at runtime) having a length of 534:

_foo.AssertWasCalled(x => x.Write(Arg.Text.Like(".{512,}")));

ExpectationViolationException: IFoo.Write(like ".{512,}"); Expected #1, Actual #0.

What have I done wrong with Like()'s pattern?

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

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

发布评论

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

评论(2

心清如水 2024-10-13 03:53:18

也许与您使用的RhinoMocks版本有关?我正在使用 RhinoMocks 版本 3.5.0.1337,Like 可以正确检测长度。

public interface IFoo
{
    void Write(string value);
}

public class Bar
{
    private readonly IFoo _foo;

    public Bar(IFoo foo)
    {
        _foo = foo;
    }
    public void Save(string value)
    {
        _foo.Write(value);
    }
}

测试

private Bar _bar;
private IFoo _foo;


[SetUp]
public void BeforeEachTest()
{
    var mocker = new RhinoAutoMocker<Bar>();
    _bar = mocker.ClassUnderTest;
    _foo = mocker.Get<IFoo>();
}


[Test]
public void Given_input_length_equal_to_that_required_by_Like()
{
    CallSave("".PadLeft(512));
}

[Test]
public void Given_input_longer_than_required_by_Like()
{
    CallSave("".PadLeft(513));
}

[Test]
[ExpectedException(typeof(ExpectationViolationException))]
public void Given_input_shorter_than_required_by_Like()
{
    CallSave("".PadLeft(511));
}

private void CallSave(string value)
{
    _bar.Save(value);
    _foo.AssertWasCalled(x => x.Write(Arg.Text.Like(".{512,}")));
}

如果我使用 .Expect() 而不是 .AssertWasCalled() 顺便说一下,测试也会通过。

private void CallSave(string value)
{
    _foo.Expect(x => x.Write(Arg.Text.Like(".{512,}")));
    _bar.Save(value);
    _foo.VerifyAllExpectations();
}

如果您通过了这些测试,并且您确定参数的长度,则通过将测试更改为

_foo.AssertWasCalled(x => x.Write(Arg<specify type here>.Is.Anything))

编辑来验证是否正在调用 Write:

RhinoMocks 版本 3.6.0.0 也通过了这些测试

Perhaps it is related to the version of RhinoMocks you are using? I'm using RhinoMocks version 3.5.0.1337 and Like detects the length correctly.

public interface IFoo
{
    void Write(string value);
}

public class Bar
{
    private readonly IFoo _foo;

    public Bar(IFoo foo)
    {
        _foo = foo;
    }
    public void Save(string value)
    {
        _foo.Write(value);
    }
}

tests

private Bar _bar;
private IFoo _foo;


[SetUp]
public void BeforeEachTest()
{
    var mocker = new RhinoAutoMocker<Bar>();
    _bar = mocker.ClassUnderTest;
    _foo = mocker.Get<IFoo>();
}


[Test]
public void Given_input_length_equal_to_that_required_by_Like()
{
    CallSave("".PadLeft(512));
}

[Test]
public void Given_input_longer_than_required_by_Like()
{
    CallSave("".PadLeft(513));
}

[Test]
[ExpectedException(typeof(ExpectationViolationException))]
public void Given_input_shorter_than_required_by_Like()
{
    CallSave("".PadLeft(511));
}

private void CallSave(string value)
{
    _bar.Save(value);
    _foo.AssertWasCalled(x => x.Write(Arg.Text.Like(".{512,}")));
}

The tests also pass if I use .Expect() instead of .AssertWasCalled() by the way.

private void CallSave(string value)
{
    _foo.Expect(x => x.Write(Arg.Text.Like(".{512,}")));
    _bar.Save(value);
    _foo.VerifyAllExpectations();
}

If these tests pass for you and you are certain about the length of the argument then verify that Write is being called by changing the test to

_foo.AssertWasCalled(x => x.Write(Arg<specify type here>.Is.Anything))

edit:

The tests also pass with RhinoMocks version 3.6.0.0

绮烟 2024-10-13 03:53:18

为什么不直接测试争论的长度

 Assert.IsTrue(Arg.Text.Length >= 512);

一般在Rhino模拟中当你得到“预期#1,实际#0”时。这意味着 Equals 存在问题,例如没有在对象上实现 equals。

Why not test the length of the arguement directly

 Assert.IsTrue(Arg.Text.Length >= 512);

Generally in Rhino mocks when you get "Expected #1, Actual #0." it means that there is a problem with Equals, for example not having implemented equals on an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文