[rspec-users] Adding stubs for an included argument

David Chelimsky dchelimsky at gmail.com
Tue Jan 5 11:54:51 EST 2010


On Tue, Jan 5, 2010 at 4:09 AM, Edvard Majakari
<edvard.majakari at gmail.com>wrote:

> Hi,
>
> 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
> follows:
>
> ...
>
> if o.allowed_for?(:foo, :bar)
>  #case a
> elsif o.allowed_for(:quux, :bif)
>  #case b
> elsif o.allowed_for(:baz)
>  #case c
> else
>  #case default
> end
>
>
> now in the spec, I'd like to say something like
>
> obj.should_allow_for(:foo)
> obj.stub!(:allowed_for? => false)
>

Try this:

obj.stub(:allowed_for) {|*args| args.include?(:foo)}

HTH,
David


>
> 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
> implementation:
>
> 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
> else
>  #case default
> end
>
> 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.
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100105/20a7e0ba/attachment.html>


More information about the rspec-users mailing list