How to view the actual value of a failed expectation

974 views
Skip to first unread message

NetSplinter

unread,
Aug 26, 2009, 3:06:36 PM8/26/09
to Rhino.Mocks
If I set an expectation on a call in a DynamicMock, when the
expectation is not met by any call, I get an exception like the below:

Rhino.Mocks.Exceptions.ExpectationViolationException :
YourClass.YourMethod(equal to Information, equal to MainOrchestration,
equal to System.Exception: foo); Expected #1, Actual #0.

Without stepping into the the test case in the debugger, is there a
way to get Rhino Mocks to include information about what calls &
parameters were made to the mocked method, so I can see why I failed
to meet the expectation just from inspecting the exception text?

Thanks!

Tim Barcz

unread,
Aug 26, 2009, 8:46:11 PM8/26/09
to rhino...@googlegroups.com
This is really hard to do.  Consider the following:

Scenario 1:
[Test]
public void Test()
{
    var view = MockRepository.GenerateMock<IFoo>();

    view.Method("tim");

    view.AssertWasCalled(x=>x.Method("tim"));
}

Scenario 2:
[Test]
public void Test()
{
    var view = MockRepository.GenerateMock<IFoo>();

    view.Method("larry");

    view.AssertWasCalled(x=>x.Method("tim"));
}

Scenario 3:
[Test]
public void Test()
{
    var view = MockRepository.GenerateMock<IFoo>();


    view.AssertWasCalled(x=>x.Method("tim"));
}

Scenario 1 passes just fine and is the example.  Scenarios 2 & 3 however will fail.

When trying to report back all RhinoMocks knows is that something didn't get called.  It doesn't know that "you were close" that your parameter was off by a single character.  The reason is it could be false positive.  In the above there are two very different things going on between #2 and #3.  In #2 the wrong param is called, in #3 the method isn't called at all. 

The best Rhino could do in my mind is tell give you a list of what was called.  It would then be up to the end user to reconcile that list to what was expected, and in some ways that's already going on when the error message tells you something was expected but never called.

Tim
--
Tim Barcz
ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

DHilgarth

unread,
Aug 30, 2009, 11:21:10 AM8/30/09
to Rhino.Mocks
IMHO it would be helpful in scenario 2, to output something like:

"
IFoo.Method(equal to tim); Expected #1, Actual #0.
However, the method has been called with these parameters:
- larry
"

On 27 Aug., 02:46, Tim Barcz <timba...@gmail.com> wrote:
> This is really hard to do.  Consider the following:
>
> *Scenario 1:*
> [Test]
> public void Test()
> {
>     var view = MockRepository.GenerateMock<IFoo>();
>
>     view.Method("tim");
>
>     view.AssertWasCalled(x=>x.Method("tim"));
>
> }
>
> *Scenario 2:*
> [Test]
> public void Test()
> {
>     var view = MockRepository.GenerateMock<IFoo>();
>
>     view.Method("larry");
>
>     view.AssertWasCalled(x=>x.Method("tim"));
>
> }
>
> *Scenario 3:*
> [Test]
> public void Test()
> {
>     var view = MockRepository.GenerateMock<IFoo>();
>
>     view.AssertWasCalled(x=>x.Method("tim"));
>
> }
>
> Scenario 1 passes just fine and is the example.  Scenarios 2 & 3 however
> will fail.
>
> When trying to report back all RhinoMocks knows is that something didn't get
> called.  It doesn't know that "you were close" that your parameter was off
> by a single character.  The reason is it could be false positive.  In the
> above there are two very different things going on between #2 and #3.  In #2
> the wrong param is called, in #3 the method isn't called at all.
>
> The best Rhino could do in my mind is tell give you a list of what was
> called.  It would then be up to the end user to reconcile that list to what
> was expected, and in some ways that's already going on when the error
> message tells you something was expected but never called.
>
> Tim
>

Stefan Steinegger

unread,
Aug 31, 2009, 2:32:34 AM8/31/09
to Rhino.Mocks
I also tried to find solutions for this, because it is a common
problem.

It is also frequently asked for, for instance here:
http://groups.google.com/group/rhinomocks/browse_thread/thread/b7db5ddb3e10c85/e346581675e73992

I wrote a patch that allowed an AssertArguments option, which let the
mock react to the call, even if the arguments are wrong, but checks
the arguments for certain values. The patch didn't get it to the
trunk, because I failed to make clear what it is good for. I should
probably try again.

Alternatively you could write something like this:

var view = MockRepository.GenerateMock<IFoo>();
view
.Stub( x => x.Method(Arg<string>.Is.Anything))
.WhenCalled( call =>
{
string arg = (string)call.Arguments[0];
Assert.AreEqual(arg, "Tim");
});

view.Method("larry");

view.AssertWasCalled(x=>x.Method("tim"));

It's a lot of not very elegant code, but does exactly what you need.
Reply all
Reply to author
Forward
0 new messages