Randy Burgess
Randy Burgess

Reputation: 5005

Receiving undefined method error in Rspec PUT Call

In my specs, when I run the POST request below, everything works fine.

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

      post :create, request_payload
    end

But when I run a spec for PUT:

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

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

I get an error like this:

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

I can't figure out what is being considered nil. This API call works fine in a REST client.

This is another error based on a previous SO question: Receiving error in RSpec for PUT, but not POST

Upvotes: 0

Views: 527

Answers (2)

Chadiso
Chadiso

Reputation: 144

I'm doing like so:

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

May be useful additionally for somebody, the way of doing similar tests;)

Upvotes: 0

apneadiving
apneadiving

Reputation: 115541

You should do :

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

Upvotes: 2

Related Questions