[rspec-users] Adding stubs for an included argument

Edvard Majakari edvard.majakari at gmail.com
Tue Jan 5 05:09:32 EST 2010


My scenario is as follows, and so far I have not found the clean way
to implement the following.

In a method I have a method called allowed_for?(*args) used as


if o.allowed_for?(:foo, :bar)
  #case a
elsif o.allowed_for(:quux, :bif)
  #case b
elsif o.allowed_for(:baz)
  #case c
  #case default

now in the spec, I'd like to say something like

obj.stub!(:allowed_for? => false)

that is, if allowed_for? is called with varargs containing :foo it
should return true, false otherwise.

At the moment the only way I can make the spec to run is to create
exact expectation, which is a bit too tightly coupled to the

obj.should_receive(:allowed_for?).with(:foo, :bar).and_return ...

another solution would wrap allow_for?(*args) into a single method
call without parameter names and stubbing the wrapper methods:

if o.fooey?
  #case a
elsif o.quuxy?
  #case b
elsif o.bazy?
  #case c
  #case default

but I don't like it - the rules are simple and the whole method is now
quite readable as it is. Creating one-line private methods would IMO
add superfluous cruft and reduce legibility.

More information about the rspec-users mailing list