在我的规范中,当我在下面运行POST请求时,一切正常。
before do
request_payload = {
player: {
first_name: "Joe",
last_name: "Carradine",
team_id: "1"
}
}
post :create, request_payload
end
但是当我为PUT运行规范时:
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 :(得分:2)
你应该这样做:
put :update, { id: 3 }.merge(request_payload)
答案 1 :(得分: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
对某些人来说可能是有用的,也就是做类似测试的方式;)