0

在我的规范中,当我运行下面的 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

4

2 回答 2

2

你应该做 :

put :update, { id: 3 }.merge(request_payload)
于 2012-10-23T15:39:49.080 回答
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

可能对某人另外有用,进行类似测试的方式;)

于 2014-03-17T20:33:01.247 回答