我想测试这个update
操作:
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
所以也应该是正确的网址,例如。
编辑:我正在使用paperclip gem作为头像。
我的routes
文件:
Rails.application.routes.draw do
get 'pages/about'
get 'matches/new'
root 'pages#home'
resources :players
resources :matches
get '*path' => redirect('/')
end
答案 0 :(得分:0)
我自己解决了这个问题。
棘手的问题是:
put :update, player: { id: player1.id, avatar: avatar }
我把它改为:
put :update, id: player1.id, player: { avatar: file }
测试通过。