For the shopping list I describe its expected that they all have the
same
expectations.
I’m still reading the rspec source.
I would think of creating a new method called:
should_receive_the_following
but seeing how the message_expectations work not sure how plausible it
would
be to implement.
and it would be much clearer if I could list them in a hash instead.
It would definitely be less typing, but I don’t think it would be as
clear in terms of intent.
We do have a similar syntax to what you’re looking for for stubs:
list = stub(“list”,
:milk => ‘milk’
)
This makes sense to me for stubs for a couple of reasons. One is that
it is similar to the syntax for creating an OpenStruct, which should
be familiar to any ruby developer.
If we were to do the same with expectations we lose two things. Most
importantly is the line number of the failure when one occurs. Also,
and_return is already problematic for me after should_receive because
it implies an expectation - it should receive this and it should
return that, as opposed to it should receive this and when it does go
ahead and return that so that the example can keep moving Combining
these two concepts into one method name would make this even more
confusing in my perception.
There are other mock frameworks that might come closer to the
terseness you desire. You should check out mocha and RR
(RubyPub.com is for sale | HugeDomains) if that’s what you’re looking for.
They both offer a different “feel” from rspec’s framework and some
different features as well.
I find this more confusing. When I first read it I thought it was
going to receive the hash as arguments and return the hash. I think
the expressiveness of “should_receive(…).and_return(…)” wins in
this case,
On Dec 30, 2007 6:52 AM, Jean-François Trân [email protected] wrote:
What about :
{
:milk => ‘milk’,
:bagel => ‘bagel’,
:coffee => ‘coffee’
}.each do |method, value| @shopping_list.should_receive(method).and_return(value)
end
So it doesn’t need a #should_receive_the_following method.
You can certainly do that (and I have), but again, you’d lose the
benefit of a unique line number in the failure message.
This is something that is often lost under the banner of DRY. DRY
doesn’t mean “type as few characters as you can.” It means don’t
repeat functionality or knowledge within a system. IMO, multiple calls
to the same methods, but with different data, are NOT duplication in
this sense.
So it doesn’t need a #should_receive_the_following method.
You can certainly do that (and I have), but again, you’d lose the
benefit of a unique line number in the failure message.
This is something that is often lost under the banner of DRY. DRY
doesn’t mean “type as few characters as you can.” It means don’t
repeat functionality or knowledge within a system. IMO, multiple calls
to the same methods, but with different data, are NOT duplication in
this sense.
(I hit send too soon)
In the end, DRY exists to serve a higher master: maintainability. And
it is not terribly rare that the illusion of DRY (as explored in this
case) flies in the face of maintainability. In this case, the clarity
of the line number in the event of a failure wins for me.
Also, this is going to have to change when you decide that the
shopping list should raise an error when it receives “bagel.” Change
is fine, but to make that change is going to be more work and likely
leave things lopsided:
{
:milk => ‘milk’,
:coffee => ‘coffee’
}.each do |method, value| @shopping_list.should_receive(method).and_return(value)
end
A Hash is not ordered.
(but the 1st set of statements is)
I don’t know if this matters in this case. RSpec doesn’t enforce
strict-order mocking. Nor does Mocha. Hardmock is the only mocking
library in ruby that I know of that can do this,
I’d like to be proven wrong again, but I don’t think you can do this
with rspec. From running some examples in irb it looks like each mock
only verifies its own order. Hardmock does support this. Flexmock does
not that I am aware of, and Mocha most certainly does not.