如何使用RSpec测试ajax POST请求?

时间:2022-08-24 04:02:40

I just want to test ajax request on controller spec. The product code is below. I'm using Devise for authentication.

我只是想在控制器规范上测试ajax请求。产品代码如下。我正在使用Devise进行身份验证。

class NotesController < ApplicationController
  def create
    if request.xhr?
      @note = Note.new(params[:note])
      if @note.save
        render json: { notice: "success" }
      end
    end
  end
end

And spec is below.

规格如下。

describe NotesController do
  before do
    user = FactoryGirl.create(:user)
    user.confirm!
    sign_in user
  end

  it "has a 200 status code" do
    xhr :post, :create, note: { title: "foo", body: "bar" }, format: :json
    response.code.should == "200"
  end
end

I expect the response code to be 200, but it returns 401. I guess it must be because the request which rspec throws lacks authenticity_token or something. How can I stub it?

我希望响应代码为200,但它返回401.我想这肯定是因为rspec抛出的请求缺少authenticity_token或其他东西。我该如何存根?

Any help would be greatly appreciated.

任何帮助将不胜感激。

3 个解决方案

#1


35  

Answering my own question. I found that format: :json was wrong. Just delete it and it works. Just like below:

回答我自己的问题。我发现格式:: json错了。只需删除它就可以了。如下所示:

it "has a 200 status code" do
  xhr :post, :create, note: { title: "foo", body: "bar" }
  response.code.should == "200"
end

I'm sorry for all the fuss.

我很抱歉所有的大惊小怪。

#2


0  

I moved format: :json into params hash and it works fine, responding with json.

我将格式:: json移动到params hash并且工作正常,用json响应。

describe NotesController do
  before do
    user = FactoryGirl.create(:user)
    user.confirm!
    sign_in user
  end

  it "has a 200 status code" do
   xhr :post, :create, { note: { title: "foo", body: "bar" }, format: :json } 
   response.code.should == "200"
  end
end

#3


-3  

You've probably met CSRF error, try to disable CSRF validation for ajax calls, something like

您可能遇到过CSRF错误,尝试禁用ajax调用的CSRF验证,例如

# In your application_controller.rb
def verified_request?
  if request.xhr?
    true
  else
    super()
  end
end

#1


35  

Answering my own question. I found that format: :json was wrong. Just delete it and it works. Just like below:

回答我自己的问题。我发现格式:: json错了。只需删除它就可以了。如下所示:

it "has a 200 status code" do
  xhr :post, :create, note: { title: "foo", body: "bar" }
  response.code.should == "200"
end

I'm sorry for all the fuss.

我很抱歉所有的大惊小怪。

#2


0  

I moved format: :json into params hash and it works fine, responding with json.

我将格式:: json移动到params hash并且工作正常,用json响应。

describe NotesController do
  before do
    user = FactoryGirl.create(:user)
    user.confirm!
    sign_in user
  end

  it "has a 200 status code" do
   xhr :post, :create, { note: { title: "foo", body: "bar" }, format: :json } 
   response.code.should == "200"
  end
end

#3


-3  

You've probably met CSRF error, try to disable CSRF validation for ajax calls, something like

您可能遇到过CSRF错误,尝试禁用ajax调用的CSRF验证,例如

# In your application_controller.rb
def verified_request?
  if request.xhr?
    true
  else
    super()
  end
end