[rspec-users] Easy AR association stubbing

Andrew WC Brown omen.king at gmail.com
Mon Oct 8 15:18:59 EDT 2007


Here's my old association:

    @game = mock_game(:to_param => "1")
    Game.stub!(:find).and_return(@game)
    @players = mock('player_proxy')
    @players.stub!(:build).and_return(mock_model(Player, :save => true))
    @game.stub!(:players).and_return(@players)

Here's one of my specs:

  it "should assign the found players for the view" do
    do_get
    assigns[:players].should eql(@players)

  end

Everything works great. So I try to use assoication_stub!

    @game = mock_game(:to_param => "1")
    Game.stub!(:find).and_return(@game)
    @game.stub_association!(:players, :build => mock_model(Player, :save =>
true))

1)
'PlayersController handling GET /saltmines/games/1/players should assign the
found players for the view' FAILED
expected nil, got #<Spec::Mocks::Mock:0x1a53358 @name="players"> (using
.eql?)
/Volumes/EXTERNAL/web/omenking.ca/spec/controllers/players_controller_spec.rb:103:
/Volumes/EXTERNAL/web/omenking.ca/spec/controllers/players_controller_spec.rb:63:



Any suggestions?

On 10/6/07, Pat Maddox <pergesu at gmail.com> wrote:
>
> On 10/6/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> > But what if I need to use posts as such?
> >
> > @person = mock_model(Person)
> > @person.stub_association!(:posts, :find_by_title => mock_model(Post))
> >
> > posts.stub!(:count).and_return(2)
> >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
>
> Take a look at the source: you can pass in whatever methods you want
> to be stubbed.  So, for example,
>
> @person.stub_association!(:posts, :find_by_title => mock_model(Post),
> :count => 2)
>
> Also, you should be able to stub after the fact like so:
>
> @person.posts.stub!(:count).and_return 2
>
> Pat
> _______________________________________________
> 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/20071008/d1015e00/attachment-0001.html 


More information about the rspec-users mailing list