2011-02-10 89 views
1

使用Rails 3,Rspec的2,factory_girl_rails寶石,我有以下代碼控制器:如何使用factory_girl測試rspec 2的rails 3控制器功能?

def remove_player 
@player = User.find(params[:id]) 
current_team.users.delete(@player) 
current_team.games.each do |g| 
    ug = GamesUser.where(:game_id => g.id, :user_id => @player.id) 
    ug.each {|x| x.destroy} 
end 
redirect_to(team_path(current_team), :notice => 'Player was successfully removed from this team') 
end 

它正常工作和做什麼,我需要做的。但是,我想不出什麼,我在我的測試中,看起來像這樣做錯了:

describe "Remove player" do 
    before do 
     @team = Factory(:team) 
     @player = Factory(:user) 
     @game = Factory(:game) 
     controller.stub!(:current_team).and_return(@team) 
    end 

    it "should remove player from team" do 
     @team.users << @player 
     @team.users.should have(1).users 
     post :remove_player, :id => @player.id 
     @team.users.should have(0).users 
    end 

    it "should remove games from player schedule" do 
     Factory(:games_user, :user_id => @player.id) 
     @player.should have(1).games 
     post :remove_player, :id => @player.id 
     @player.should have(0).games  
    end 
end 

第一個測試通過。然而第二個沒有。我對rspec和新測試一般都很陌生,所以請隨時指出你所看到的任何缺陷 - 我不會被冒犯。

我在這裏嘗試做的要點是從一個團隊(團隊has_many用戶)中刪除一個玩家,並從該玩家的時間表中刪除任何遊戲(遊戲habtm用戶通過GamesUser)。哦,「current_team」是一個幫助方法,它根據會話變量提取Team。

+0

我看到的主要缺陷是存在控制器規格。 :)恕我直言RSpec是偉大的模型,但RSpec控制器規格是痛苦的 - 使用黃瓜代替。 – 2011-11-14 22:10:17

回答

0

如果你仔細對比你的說法,您呼叫的:在第一種情況下「@team」的has_many「用戶」,

@team.users.should have(0).users 

調用:的has_many的「遊戲」 「@player」

@player.should have(0).games 

不看的文件(我可能是錯的),我想在have(1).games的「遊戲」只是語法糖。請驗證並讓我知道。

相關問題