Forum: RSpec Easy AR association stubbing

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.
208c855e1ed834f1d938e23fe747a549?d=identicon&s=25 Matthew Heidemann (Guest)
on 2007-10-05 07:43
(Received via mailing list)
I've added a method to the mock class that makes it pretty easy to
stub associations in rails. I've been using it for awhile and it seems
to cut down on a lot of setup code for the controller and model specs
that use associations.

#before
@person = mock_model(Person)
posts = mock('post_proxy')
posts.stub!(:build).and_return(mock_model(Post, :save => true))
@person.stub!(:posts).and_return(posts)


# now
@person = mock_model(Person)
@person.stub_association!(:posts, :find_by_title => mock_model(Post))


Just add this to the spec helper

module Spec
  module Mocks
    module Methods
      def stub_association!(association_name, methods_to_be_stubbed =
{})
        mock_association = Spec::Mocks::Mock.new(association_name.to_s)
        methods_to_be_stubbed.each do |method, return_value|
          mock_association.stub!(method).and_return(return_value)
        end
        self.stub!(association_name).and_return(mock_association)
      end
    end
  end
end
Dd0eeebd59d932e3d1a1bc55a92e9205?d=identicon&s=25 Andrew Brown (omenking)
on 2007-10-06 03:59
(Received via mailing list)
Thanks I'll give it a try and praise you later.
Dd0eeebd59d932e3d1a1bc55a92e9205?d=identicon&s=25 Andrew Brown (omenking)
on 2007-10-06 23:45
(Received via mailing list)
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)
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-10-07 00:12
(Received via mailing list)
On 10/6/07, Andrew WC Brown <omen.king@gmail.com> wrote:
> 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
Dd0eeebd59d932e3d1a1bc55a92e9205?d=identicon&s=25 Andrew Brown (omenking)
on 2007-10-08 21:26
(Received via mailing list)
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?
208c855e1ed834f1d938e23fe747a549?d=identicon&s=25 Matthew Heidemann (Guest)
on 2007-10-10 10:05
(Received via mailing list)
you no longer have @players

try
assigns[:players].should eql(@game.players)
This topic is locked and can not be replied to.