2011-02-10 6 views
1

使用して、レール3、RSpecの2が、私は次のコードでコントローラを持って、宝石をfactory_girl_rails:factory_girlを使用してrspec 2でこのレール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 

最初のテストに合格します。しかし、第2のものはそうではない。私はrspecとテスト全般に慣れていませんので、あなたが見ている欠陥を指摘してください - 私は怒らないでください。

私がここでやろうとしていることの要点は、チームからプレーヤーを削除し(チームhas_manyユーザ)、そのプレイヤーのスケジュールからゲームを削除することです(GameUserを通してGame habtm Users)。 Ohと "current_team"は、セッション変数に基づいてチームをフェッチするヘルパーメソッドです。

+0

私が見る主な欠陥は、コントローラーの仕様の存在です。 :) IMHO RSpecはモデルには最適ですが、RSpecコントローラ仕様は痛いです。代わりにキュウリを使用してください。 –

答えて

0

あなたは慎重にあなたのアサーションを比較すると、あなたが呼んでいる:上has_manyの「ゲーム」:呼び出していない

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

しかし、最初のケースでは、「@team」上にhas_manyに「ユーザ」を(私は間違っているかもしれない)私はhave(1).gamesで「ゲーム」は単なる糖衣構文だと思うのドキュメントを見ずに「@player」

@player.should have(0).games 

。確認してお知らせください。

関連する問題