[rspec-users] spec'ing views, mock_model associations

Andrew WC Brown omen.king at gmail.com
Sat Oct 6 14:41:31 EDT 2007


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 


More information about the rspec-users mailing list