1

I've written an update action with the Grape API in rails, shown here:

put do
  work = Work.find(permitted_params[:id])
  work.title = permitted_params[:title]
  work.date = permitted_params[:date]

  work.save!
end

Testing it with the swagger documentation in my browser, I get success.

I have a spec for it that isn't working quite right. Despite the success in the browser, my test fails because it says it's getting a 405 in the response. Here's the spec:

describe "PUT /api/v1/works/work_id" do
  let(:user) { FactoryGirl.create(:user) }
  let(:collection) { FactoryGirl.create(:collection, user: user) }
  let(:work) { FactoryGirl.create(:work, collection: collection) }
  let(:work_data) do
    {
      collection: collection.id,
      title: "Lorem ipsum dolor sit amet",
      date: 1492,
      place: "Chicago",
    }
  end 

  describe "Modify title" do     
    let(:work_title) { "gfd" }

    before :each do
      work_data[:title] = work_title
      put "/api/v1/works/#{work.id}", work_data
      work.reload
    end

    it "returns success" do
      expect(response).to be_success
    end

    it "updates the title of the work" do
      expect(work.title).to eql(work_title)
    end
  end
end 

I'm curious why it seems to be working correctly, but I'm still getting a 405 error in my test.

Edit to include response from the server 405 {"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "Allow"=>"OPTIONS, GET, HEAD", "Content-Type"=>"text/plain", "Content-Length"=>"0", "Cache-Control"=>"no-cache", "X-Request-Id"=>"5edd38be-289d-4e97-875d-2461389a746d", "X-Runtime"=>"0.088867"} #

amhasler
  • 67
  • 1
  • 6
  • please post the test logs for the above requests. They are available in `logs/test.log` – bsvin33t Apr 07 '15 at 05:43
  • That seems like I mostly junk that I can't really make sense of. Is there a gem I can add to improve that readout? I was able to ascertain the 405 error just by printing out the response in the log. – amhasler Apr 08 '15 at 13:37
  • You can delete the log and re-run a single spec that gives you a 405. From this, you will see the failure message for a single request. – bsvin33t Apr 08 '15 at 13:39
  • Duplicate of http://stackoverflow.com/q/18139135/216513 ? – nathanvda Apr 08 '15 at 22:31

1 Answers1

2

I guess this is answer is relevant: https://stackoverflow.com/a/18064204/216513

Without parameters this will become a put on index, and thus not allowed. So you need to specify the content type for the parameters to be considered.

Although there is a very similar question with a different answer: PUT method return 405 in RSpec test for API

Community
  • 1
  • 1
nathanvda
  • 49,707
  • 13
  • 117
  • 139