使用Grape API发出请求只能使用RSPEC收到405错误

时间:2015-04-06 21:29:23

标签: ruby-on-rails ruby rspec grape

我已经在rails中使用Grape API编写了更新操作,如下所示:

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

  work.save!
end

在浏览器中使用swagger文档对其进行测试,我获得了成功。

我有一个规格,它不是很正常。尽管浏览器取得了成功,但我的测试失败了,因为它说它在响应中获得了405。这是规范:

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 

我很好奇为什么它似乎工作正常,但我的测试中仍然出现405错误。

编辑以包含来自服务器的响应     405     {" X-Frame-Options" =>" SAMEORIGIN"," X-XSS-Protection" =>" 1; mode = block"," X-Content-Type-Options" =>" nosniff","允许" =>" OPTIONS, GET,HEAD"," Content-Type" =>" text / plain"," Content-Length" =>" 0&# 34;," Cache-Control" =>" no-cache"," X-Request-Id" =>" 5edd38be-289d- 4e97-875d-2461389a746d"," X-Runtime" =>" 0.088867"}     #

1 个答案:

答案 0 :(得分:1)

我想这是答案是相关的:https://stackoverflow.com/a/18064204/216513

如果没有参数,这将成为一个放置索引,因此不允许。因此,您需要指定要考虑的参数的内容类型。

虽然有一个非常相似的问题,答案不同:PUT method return 405 in RSpec test for API