2015-09-02 42 views
1

更新頭像我要測試的這款update行動:試驗中的RSpec

def update 
    @player = Player.find(params[:id]) 
    begin 
     @player.update_attributes(player_params_avatar) 
     flash[:success] = "Avatar updated" 
     redirect_to player_path(@player) 
    rescue ActionController::ParameterMissing => e 
     flash[:danger] = "First choose avatar file" 
     redirect_to :back 
    end 
    end 

def player_params_avatar 
    params.require(:player).permit(:avatar) 
end 

我想是這樣的:

describe "PUT #update" do 
    let!(:player1) { FactoryGirl.create :player } 
    context "when data is valid" do 
     it "update avatar" do 
     avatar = File.new(Rails.root + 'spec/factories/images/1.png') 
     put :update, player: { id: player1.id, avatar: avatar } 
     expect(flash[:success]).to be_present 
     end 
    end 

但RSpec的控制檯我得到的錯誤:

Failure/Error: put :update, player: { id: player1.id, avatar: avatar } 
ActionController::UrlGenerationError: 
    No route matches {:action=>"update", :controller=>"players", :player=>{:id=>"1", :avatar=>"#<File:0xaed5b84>"}} 

爲什麼這段代碼會出現這個錯誤?有特定的球員id所以應該也是正確的網址例如。

http://localhost:3000/players/1/edit

編輯:我使用回形針寶石的化身。

routes文件:

Rails.application.routes.draw do 
    get 'pages/about' 
    get 'matches/new' 

    root 'pages#home' 

    resources :players 
    resources :matches 

    get '*path' => redirect('/') 
end 
+0

路由是否正確設置? – apneadiving

+0

是的,在瀏覽器中我可以更新頭像,一切都OK。 – Jensky

回答

0

我解決了自己的問題。

棘手的路線是:

put :update, player: { id: player1.id, avatar: avatar } 

我把它改爲:

put :update, id: player1.id, player: { avatar: file } 

並測試通過。