Andrew WC Brown
2007-Oct-06 18:31 UTC
[rspec-users] spec''ing views, mock_model associations
I writing a spec that returns the count of how many players in a game:
it "should how many players in a link to all players" do
render :partial =>"games/game", :object => @game
response.should have_tag(''a'',"(2) Players")
end
I''m not sure how to do the mock model, I think it would be done two
ways but
unsure of the syntax:
1: add players in
@game = mock_model(Game,
:name => ''The Battle for Blaze'',
:salt_grains => ''5000000'',
:people => ''5000000'',
:days => nil,
:created_at => "Mon Oct 01 00:02:44 -0400 2007",
:enabled => true,
:players => {
})
2: assign them to players
@game = mock_model(Game,
:name => ''The Battle for Blaze'',
:salt_grains => ''5000000'',
:people => ''5000000'',
:days => nil,
:created_at => "Mon Oct 01 00:02:44 -0400 2007",
:enabled => true)
player_1 = mock_model(Player, :salt_grains => ''500'')
player_2 = mock_model(Player, :salt_grains => ''900'')
@game.players = [ player_1, player_2 ]
How do I write this? I would need an example
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20071006/053aebb6/attachment.html
Andrew WC Brown
2007-Oct-06 18:41 UTC
[rspec-users] spec''ing views, mock_model associations
I for some reason think I would use stub such as:
@game = mock_model(Game,
:name => ''The Battle for Blaze'',
:salt_grains => ''5000000'',
:people => ''5000000'',
:days => nil,
:created_at => "Mon Oct 01 00:02:44 -0400 2007",
:enabled => true)
player_1 = mock_model(Player, :salt_grains => ''500'')
player_2 = mock_model(Player, :salt_grains => ''900'')
players = [ player_1, player_2 ]
@game.stub!(:players).and_return(players)
but it doesn''t work out:
1)
ActionView::TemplateError in ''/games/_game.rhtml should show game
name''
undefined method `count'' for #<Array:0x30db51c>
On line #4 of app/views/games/_game.rhtml
1: <div>
2:
3: <%= link_to game.name, new_player_path(game) %>
4: <%= link_to "(#{game.players.count}) Players",
"google.com" %>
5: <p>Salt unmined:<span><%= h game.salt_grains
%></span></p>
6: <%= h game.people %>
7: <%= h game.enabled %>
On 10/6/07, Andrew WC Brown <omen.king at gmail.com>
wrote:>
> I writing a spec that returns the count of how many players in a game:
>
> it "should how many players in a link to all players" do
> render :partial =>"games/game", :object => @game
> response.should have_tag(''a'',"(2) Players")
> end
>
> I''m not sure how to do the mock model, I think it would be done
two ways
> but unsure of the syntax:
>
>
> 1: add players in
>
> @game = mock_model(Game,
> :name => ''The Battle for Blaze'',
> :salt_grains => ''5000000'',
> :people => ''5000000'',
> :days => nil,
> :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> :enabled => true,
> :players => {
>
> })
>
> 2: assign them to players
>
> @game = mock_model(Game,
> :name => ''The Battle for Blaze'',
> :salt_grains => ''5000000'',
> :people => ''5000000'',
> :days => nil,
> :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> :enabled => true)
>
> player_1 = mock_model(Player, :salt_grains => ''500'')
> player_2 = mock_model(Player, :salt_grains => ''900'')
>
> @game.players = [ player_1, player_2 ]
>
> How do I write this? I would need an example
--
Monsterbox Productions
putting small businesses on-line
1319 Victoria Avenue East
Thunder Bay, Ontario P7C 1C3
Canada
Andrew WC Brown
web-developer and owner
andrew at monsterboxpro.com
P: 807-626-9009
F: 807-624-2705
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20071006/3f5b985b/attachment.html
Andrew WC Brown
2007-Oct-06 18:50 UTC
[rspec-users] spec''ing views, mock_model associations
Oh so I have to mock post_proxy.
I think I''m getting closer
@game = mock_model(Game,
:name => ''The Battle for Blaze'',
:salt_grains => ''5000000'',
:people => ''5000000'',
:days => nil,
:created_at => "Mon Oct 01 00:02:44 -0400 2007",
:enabled => true)
#player_1 = mock_model(Player, :salt_grains => ''500'')
#player_2 = mock_model(Player, :salt_grains => ''900'')
#players = [ player_1, player_2 ]
players = mock(''post_proxy'')
players.stub!(:build).and_return(mock_model(Player, :save => true))
@game.stub!(:players).and_return(players)
although:
ActionView::TemplateError in ''/games/_game.rhtml should show game
name''
Mock ''post_proxy'' received unexpected message :count with (no
args)
On line #4 of app/views/games/_game.rhtml
1: <div>
2:
3: <%= link_to game.name, new_player_path(game) %>
4: <%= link_to "(#{game.players.count}) Players",
"google.com" %>
5: <p>Salt unmined:<span><%= h game.salt_grains
%></span></p>
6: <%= h game.people %>
7: <%= h game.enabled %>
On 10/6/07, Andrew WC Brown <omen.king at gmail.com>
wrote:>
> I for some reason think I would use stub such as:
>
> @game = mock_model(Game,
> :name => ''The Battle for Blaze'',
> :salt_grains => ''5000000'',
> :people => ''5000000'',
> :days => nil,
> :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> :enabled => true)
>
> player_1 = mock_model(Player, :salt_grains =>
''500'')
> player_2 = mock_model(Player, :salt_grains =>
''900'')
> players = [ player_1, player_2 ]
>
> @game.stub!(:players).and_return(players)
>
> but it doesn''t work out:
> 1)
> ActionView::TemplateError in ''/games/_game.rhtml should show game
name''
> undefined method `count'' for #<Array:0x30db51c>
> On line #4 of app/views/games/_game.rhtml
>
> 1: <div>
> 2:
> 3: <%= link_to game.name, new_player_path(game) %>
> 4: <%= link_to "(#{game.players.count}) Players",
"google.com" %>
> 5: <p>Salt unmined:<span><%= h game.salt_grains
%></span></p>
> 6: <%= h game.people %>
> 7: <%= h game.enabled %>
>
> On 10/6/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> >
> > I writing a spec that returns the count of how many players in a game:
> >
> > it "should how many players in a link to all players" do
> > render :partial =>"games/game", :object => @game
> > response.should have_tag(''a'',"(2)
Players")
> > end
> >
> > I''m not sure how to do the mock model, I think it would be
done two ways
> > but unsure of the syntax:
> >
> >
> > 1: add players in
> >
> > @game = mock_model(Game,
> > :name => ''The Battle for Blaze'',
> > :salt_grains => ''5000000'',
> > :people => ''5000000'',
> > :days => nil,
> > :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> > :enabled => true,
> > :players => {
> >
> > })
> >
> > 2: assign them to players
> >
> > @game = mock_model(Game,
> > :name => ''The Battle for Blaze'',
> > :salt_grains => ''5000000'',
> > :people => ''5000000'',
> > :days => nil,
> > :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> > :enabled => true)
> >
> > player_1 = mock_model(Player, :salt_grains =>
''500'')
> > player_2 = mock_model(Player, :salt_grains =>
''900'')
> >
> > @game.players = [ player_1, player_2 ]
> >
> > How do I write this? I would need an example
>
>
>
>
> --
> Monsterbox Productions
> putting small businesses on-line
>
> 1319 Victoria Avenue East
> Thunder Bay, Ontario P7C 1C3
> Canada
>
> Andrew WC Brown
> web-developer and owner
> andrew at monsterboxpro.com
> P: 807-626-9009
> F: 807-624-2705
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20071006/5f568a91/attachment-0001.html
Andrew WC Brown
2007-Oct-06 18:54 UTC
[rspec-users] spec''ing views, mock_model associations
I made it pass!
I just had to do some stubbing.
@game = mock_model(Game,
:name => ''The Battle for Blaze'',
:salt_grains => ''5000000'',
:people => ''5000000'',
:days => nil,
:created_at => "Mon Oct 01 00:02:44 -0400 2007",
:enabled => true)
#player_1 = mock_model(Player, :salt_grains => ''500'')
#player_2 = mock_model(Player, :salt_grains => ''900'')
#players = [ player_1, player_2 ]
players = mock(''post_proxy'')
players.stub!(:build).and_return(mock_model(Player, :save => true))
@game.stub!(:players).and_return(players)
players.stub!(:count).and_return(2)
On 10/6/07, Andrew WC Brown <omen.king at gmail.com>
wrote:>
> Oh so I have to mock post_proxy.
> I think I''m getting closer
>
> @game = mock_model(Game,
> :name => ''The Battle for Blaze'',
> :salt_grains => ''5000000'',
> :people => ''5000000'',
> :days => nil,
> :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> :enabled => true)
>
> #player_1 = mock_model(Player, :salt_grains =>
''500'')
> #player_2 = mock_model(Player, :salt_grains =>
''900'')
> #players = [ player_1, player_2 ]
>
> players = mock(''post_proxy'')
> players.stub!(:build).and_return(mock_model(Player, :save => true))
> @game.stub!(:players).and_return(players)
>
>
> although:
>
> ActionView::TemplateError in ''/games/_game.rhtml should show game
name''
> Mock ''post_proxy'' received unexpected message :count with
(no args)
> On line #4 of app/views/games/_game.rhtml
>
> 1: <div>
> 2:
> 3: <%= link_to game.name, new_player_path(game) %>
> 4: <%= link_to "(#{game.players.count}) Players", "
google.com" %>
> 5: <p>Salt unmined:<span><%= h game.salt_grains
%></span></p>
> 6: <%= h game.people %>
> 7: <%= h game.enabled %>
>
> On 10/6/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> >
> > I for some reason think I would use stub such as:
> >
> > @game = mock_model(Game,
> > :name => ''The Battle for Blaze'',
> > :salt_grains => ''5000000'',
> > :people => ''5000000'',
> > :days => nil,
> > :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> > :enabled => true)
> >
> > player_1 = mock_model(Player, :salt_grains =>
''500'')
> > player_2 = mock_model(Player, :salt_grains =>
''900'')
> > players = [ player_1, player_2 ]
> >
> > @game.stub!(:players).and_return(players)
> >
> > but it doesn''t work out:
> > 1)
> > ActionView::TemplateError in ''/games/_game.rhtml should show
game name''
> > undefined method `count'' for #<Array:0x30db51c>
> > On line #4 of app/views/games/_game.rhtml
> >
> > 1: <div>
> > 2:
> > 3: <%= link_to game.name, new_player_path(game) %>
> > 4: <%= link_to "(#{game.players.count}) Players",
"google.com" %>
> > 5: <p>Salt unmined:<span><%= h game.salt_grains
%></span></p>
> > 6: <%= h game.people %>
> > 7: <%= h game.enabled %>
> >
> > On 10/6/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> > >
> > > I writing a spec that returns the count of how many players in a
game:
> > >
> > >
> > > it "should how many players in a link to all players"
do
> > > render :partial =>"games/game", :object => @game
> > > response.should have_tag(''a'',"(2)
Players")
> > > end
> > >
> > > I''m not sure how to do the mock model, I think it would
be done two
> > > ways but unsure of the syntax:
> > >
> > >
> > > 1: add players in
> > >
> > > @game = mock_model(Game,
> > > :name => ''The Battle for Blaze'',
> > > :salt_grains => ''5000000'',
> > > :people => ''5000000'',
> > > :days => nil,
> > > :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> > > :enabled => true,
> > > :players => {
> > >
> > > })
> > >
> > > 2: assign them to players
> > >
> > > @game = mock_model(Game,
> > > :name => ''The Battle for Blaze'',
> > > :salt_grains => ''5000000'',
> > > :people => ''5000000'',
> > > :days => nil,
> > > :created_at => "Mon Oct 01 00:02:44 -0400 2007",
> > > :enabled => true)
> > >
> > > player_1 = mock_model(Player, :salt_grains =>
''500'')
> > > player_2 = mock_model(Player, :salt_grains =>
''900'')
> > >
> > > @game.players = [ player_1, player_2 ]
> > >
> > > How do I write this? I would need an example
> >
> >
> >
> >
> > --
> > Monsterbox Productions
> > putting small businesses on-line
> >
> > 1319 Victoria Avenue East
> > Thunder Bay, Ontario P7C 1C3
> > Canada
> >
> > Andrew WC Brown
> > web-developer and owner
> > andrew at monsterboxpro.com
> > P: 807-626-9009
> > F: 807-624-2705
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20071006/5052e51f/attachment.html
David Chelimsky
2007-Oct-06 21:12 UTC
[rspec-users] spec''ing views, mock_model associations
On 10/6/07, Andrew WC Brown <omen.king at gmail.com> wrote:> I made it pass!Glad to be of help!> I just had to do some stubbing. > > @game = mock_model(Game, > :name => ''The Battle for Blaze'', > :salt_grains => ''5000000'', > :people => ''5000000'', > :days => nil, > :created_at => "Mon Oct 01 00:02:44 -0400 2007", > :enabled => true) > > #player_1 = mock_model(Player, :salt_grains => ''500'') > #player_2 = mock_model(Player, :salt_grains => ''900'') > #players = [ player_1, player_2 ] > > players = mock(''post_proxy'') > players.stub!(:build).and_return(mock_model(Player, > :save => true)) > @game.stub!(:players).and_return(players) > players.stub!(:count).and_return(2) > > > > On 10/6/07, Andrew WC Brown <omen.king at gmail.com> wrote: > > Oh so I have to mock post_proxy. > > I think I''m getting closer > > > > @game = mock_model(Game, > > :name => ''The Battle for Blaze'', > > :salt_grains => ''5000000'', > > :people => ''5000000'', > > :days => nil, > > :created_at => "Mon Oct 01 00:02:44 -0400 2007", > > :enabled => true) > > > > #player_1 = mock_model(Player, :salt_grains => ''500'') > > #player_2 = mock_model(Player, :salt_grains => ''900'') > > #players = [ player_1, player_2 ] > > > > players = mock(''post_proxy'') > > players.stub!(:build).and_return(mock_model(Player, > :save => true)) > > @game.stub!(:players).and_return(players) > > > > > > although: > > > > ActionView::TemplateError in ''/games/_game.rhtml should show game name'' > > Mock ''post_proxy'' received unexpected message :count with (no args) > > > > On line #4 of app/views/games/_game.rhtml > > > > 1: <div> > > 2: > > 3: <%= link_to game.name, new_player_path(game) %> > > 4: <%= link_to "(#{ game.players.count}) Players", " google.com" %> > > 5: <p>Salt unmined:<span><%= h game.salt_grains %></span></p> > > 6: <%= h game.people %> > > 7: <%= h game.enabled %> > > > > > > On 10/6/07, Andrew WC Brown <omen.king at gmail.com> wrote: > > > I for some reason think I would use stub such as: > > > > > > @game = mock_model(Game, > > > :name => ''The Battle for Blaze'', > > > :salt_grains => ''5000000'', > > > :people => ''5000000'', > > > :days => nil, > > > :created_at => "Mon Oct 01 00:02:44 -0400 2007", > > > :enabled => true) > > > > > > player_1 = mock_model(Player, :salt_grains => ''500'') > > > player_2 = mock_model(Player, :salt_grains => ''900'') > > > players = [ player_1, player_2 ] > > > > > > @game.stub!(:players).and_return(players) > > > > > > but it doesn''t work out: > > > 1) > > > ActionView::TemplateError in ''/games/_game.rhtml should show game name'' > > > undefined method `count'' for #<Array:0x30db51c> > > > On line #4 of app/views/games/_game.rhtml > > > > > > 1: <div> > > > 2: > > > 3: <%= link_to game.name, new_player_path(game) %> > > > 4: <%= link_to "(#{game.players.count}) Players", "google.com" %> > > > 5: <p>Salt unmined:<span><%= h game.salt_grains %></span></p> > > > 6: <%= h game.people %> > > > 7: <%= h game.enabled %> > > > > > > > > > > > > On 10/6/07, Andrew WC Brown <omen.king at gmail.com > wrote: > > > > I writing a spec that returns the count of how many players in a game: > > > > > > > > it "should how many players in a link to all players" do > > > > render :partial =>"games/game", :object => @game > > > > response.should have_tag(''a'',"(2) Players") > > > > end > > > > > > > > I''m not sure how to do the mock model, I think it would be done two > ways but unsure of the syntax: > > > > > > > > > > > > 1: add players in > > > > > > > > @game = mock_model(Game, > > > > :name => ''The Battle for Blaze'', > > > > :salt_grains => ''5000000'', > > > > :people => ''5000000'', > > > > :days => nil, > > > > :created_at => "Mon Oct 01 00:02:44 -0400 2007", > > > > :enabled => true, > > > > :players => { > > > > > > > > }) > > > > > > > > 2: assign them to players > > > > > > > > @game = mock_model(Game, > > > > :name => ''The Battle for Blaze'', > > > > :salt_grains => ''5000000'', > > > > :people => ''5000000'', > > > > :days => nil, > > > > :created_at => "Mon Oct 01 00:02:44 -0400 2007", > > > > :enabled => true) > > > > > > > > player_1 = mock_model(Player, :salt_grains => ''500'') > > > > player_2 = mock_model(Player, :salt_grains => ''900'') > > > > > > > > @game.players = [ player_1, player_2 ] > > > > > > > > How do I write this? I would need an example > > > > > > > > > > > > -- > > > Monsterbox Productions > > > putting small businesses on-line > > > > > > 1319 Victoria Avenue East > > > Thunder Bay, Ontario P7C 1C3 > > > Canada > > > > > > Andrew WC Brown > > > web-developer and owner > > > andrew at monsterboxpro.com > > > P: 807-626-9009 > > > F: 807-624-2705 > > > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >