Forum: RSpec stub! and null_object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-11-04 06:22
(Received via mailing list)
I have some code I want to stub out as it's not part of what I'm
testing.

It's got a bit of DSL going on, so there are method calls chained
together.

The call I want to stub out looks like this:

     user.record_action(:uploaded_a_photo => concert).with_result(photo)

So ideally, I'd just do @user.stub!(:record_action) and that would
return a null object that didn't care what we did to it.

However, possibly due to my own ignorance, I'm having to do something
a lot more ugly, like this:

     @user.stub!(:record_action).and_return(mock('blah',:null_object
=> true))

Can anyone beautify this a bit more?

cheers,
Matt
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-04 06:37
(Received via mailing list)
On Mon, Nov 3, 2008 at 5:36 AM, Matt Wynne <matt@mattwynne.net> wrote:
> I have some code I want to stub out as it's not part of what I'm testing.
>
> It's got a bit of DSL going on, so there are method calls chained together.
>
> The call I want to stub out looks like this:
>
>    user.record_action(:uploaded_a_photo => concert).with_result(photo)
>
> So ideally, I'd just do @user.stub!(:record_action) and that would return a
> null object that didn't care what we did to it.

stub! returns self (@user in this case), that's how it's able to chain
methods like and_return

>
> However, possibly due to my own ignorance, I'm having to do something a lot
> more ugly, like this:
>
>    @user.stub!(:record_action).and_return(mock('blah',:null_object => true))
>
> Can anyone beautify this a bit more?

Only slightly:

@user.stub!(:record_action).and_return(mock('blah').as_null_object)
This topic is locked and can not be replied to.