2012-10-23 43 views
0

在我的規範中,當我在下面運行POST請求時,一切正常。在Rspec PUT中接收未定義的方法錯誤調用

before do 
     request_payload = { 
     player: { 
      first_name: "Joe", 
      last_name: "Carradine", 
      team_id: "1" 
     } 
     } 

     post :create, request_payload 
    end 

但是當我運行了把規格:

before do 
     request_payload = { 
     player: { 
      first_name: "Buck", 
      last_name: "Carradine", 
      team_id: "1" 
     } 
     } 

     put :update, { id: 3 }, request_payload 
    end 

我得到這樣的錯誤:

Failure/Error: put :update, { id: 1 }, request_payload 
NoMethodError: 
    undefined method `[]' for nil:NilClass 

我無法弄清楚什麼是被認爲是零。此API調用在REST客戶端中正常工作。

這是基於以前的一個SO問題的另一個錯誤:Receiving error in RSpec for PUT, but not POST

+0

如果你這樣做:'put:update,{id:3} .merg e(request_payload)' – apneadiving

+0

這工作(謝謝,你可以添加爲答案?),但鑑於id哈希代表的值來自URL而不是有效載荷,這種技術是一個很好的測試? – wrburgess

+1

實際上,通過表單傳遞的每一個數據都可以通過參數獲得,所以你不會在那裏作弊。 – apneadiving

回答

2

你應該這樣做:

put :update, { id: 3 }.merge(request_payload) 
0

我做像這樣:

describe 'PUT #update' do 

before do 

    @todo = FactoryGirl.create(:todo) 

    @initial_title = @todo.title 
    @initial_updated_at = @todo.updated_at 
    @new_title = 'Title Changed' 

    request_payload = { :title => @new_title } 

    put :update, :id => @todo.id, :todo => request_payload, :format => :json 

    @todo.reload 

end 

it 'should retrieve status code of 204' do 
    response.status.should eq(204) 
end 

it 'updated attributes should not be as initially' do 
    @todo.title.should_not eq(@initial_title) 
    @todo.updated_at.should_not eq(@initial_updated_at) 
end 

it 'updated attribute should be the the same' do 
    @todo.title.should eq(@new_title) 
end 

it 'updated date should be increased' do 
    @todo.updated_at.should > @initial_updated_at 
end 


end 

可能是有用的附加爲有人,做類似測試的方式;)

相關問題