Search code examples
c#design-patternsargumentsrhino-mocks

RhinoMocks/AssertWasCalled: verifying argument length?


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?


Solution

  • 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