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
Thanks I''ll give it a try and praise you later. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071005/6ae895bd/attachment.html
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) -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071006/0d1ca9da/attachment.html
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
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
you no longer have @players try assigns[:players].should eql(@game.players) On 10/8/07, Andrew WC Brown <omen.king at gmail.com> wrote:> 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 > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 10/5/07, Andrew WC Brown <omen.king at gmail.com> wrote:> 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.Hi Matthew, Thanks for this tip. I tried refactoring my subdomain as account keys spec (http://rubyforge.org/pipermail/rspec-users/2007-October/ 004157.html) with stub_association!, but am having some trouble. Here''s my setup method: before do @request.host = "subdomain.test.host" @item = mock_model(Item) Item.stub!(:find).and_return(@item) @current_company = mock_model(Company) @current_company.stub_association!(:items, :find => [@item]) end it "should find the item requested" do @current_company.should_receive(:find).with("1").and_return(@item) do_get end ... fails with: Mock ''Company_1005'' expected :find with ("1") once, but received it 0 times I''m using subdomains as account keys, as in my code example here: http://rubyforge.org/pipermail/rspec-users/2007-October/004157.html Thanks! - Ryan Heneise
try: it "should find the item requested" do
@current_company.items.should_receive(:find).with("1").and_return(@item)
do_get
end
-Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20071026/8a66c728/attachment.html