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

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 [email protected] wrote:

:players => {
: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
[email protected]
P: 807-626-9009
F: 807-624-2705

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 %>

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 [email protected] wrote:

I made it pass!

Glad to be of help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs