Here's a question that came up at work today. One team wanted to assert in a test that the target code would call a service three times and get back a series of values. Their first thought was something like this:
thing.expects(once()).method("foo").will(returnValue(99)).id("first");
thing.expects(once()).method("foo").after("first").will(returnValue(21)).id("second");
thing.expects(once()).method("foo").after("second").will(returnValue(17));
which uses the expectation identifier to enforce sequence. A more compact version looks like this:
thing.expects(exactly(3)).method("foo")
.will(onConsecutiveCalls(returnValue(99), returnValue(21), returnValue(17)));
This will fire each stub (
returnValue()
) in turn when
foo()
is invoked, in this case returning the series of values.
Now, after the fact, I wonder if they should have stubbed the call:
thing.stubs().method("foo").will(onConsecutiveCalls(returnValue(99), returnValue(21), returnValue(17)));
It looks like the call to
foo()
is supporting infrastructure, not part of the specification of the behaviour of the target code. I'll have to check in the morning.
No comments:
Post a Comment