如何在RSpec中使用HTTP状态代码符号?

JJD*_*JJD 27 rspec ruby-on-rails http-status-codes human-readable

我在控制器的代码中使用HTTP状态代码符号,例如:

render json: {
    auth_token: user.authentication_token, 
    user: user
  }, 
  status: :created
Run Code Online (Sandbox Code Playgroud)

要么

render json: {
    errors: ["Missing parameter."]
  }, 
  success: false, 
  status: :unprocessable_entity
Run Code Online (Sandbox Code Playgroud)

在我的请求规范的代码中我也想使用符号:

post user_session_path, email: @user.email, password: @user.password
expect(last_response.status).to eq(201)
Run Code Online (Sandbox Code Playgroud)

...

expect(last_response.status).to eq(422)
Run Code Online (Sandbox Code Playgroud)

但是,我使用符号而不是整数的每个测试都会失败:

Failure/Error: expect(last_response.status).to eq(:created)

  expected: :created
       got: 201

  (compared using ==)
Run Code Online (Sandbox Code Playgroud)

以下是Rack中最新的HTTP状态代码符号列表.

cyr*_*ier 21

这对我有用:

expect(response.response_code).to eq(Rack::Utils::SYMBOL_TO_STATUS_CODE[:not_found])
Run Code Online (Sandbox Code Playgroud)


Aar*_*n K 21

response对象响应几种符号类型作为消息.所以你可以简单地做:

expect(response).to be_success
expect(response).to be_error
expect(response).to be_missing
expect(response).to be_redirect
Run Code Online (Sandbox Code Playgroud)

对于其他类型,例如:created,您可以为此创建一个简单的自定义匹配器,它包装assert_response:

RSpec::Matchers.define :have_status do |type, message = nil|
  match do |_response|
    assert_response type, message
  end
end

expect(response).to have_status(:created)
expect(response).to have_status(404)
Run Code Online (Sandbox Code Playgroud)

对于具有正确状态设置的控制器规格,这应该可以正常工作.它不适用于功能规格.我没有尝试过请求规格,所以你的milage可能会有所不同.

其工作原因是它利用了RSpec控制器规范在幕后具有类似状态设置的事实.所以当assert_response访问时@response它是可用的.

只需将使用的代码复制到匹配器中,就可以改进此匹配assert_response器:

RSpec::Matchers.define :have_status do |type, message = nil|
  match do |response|
    if Symbol === type
      if [:success, :missing, :redirect, :error].include?(type)
        response.send("#{type}?")
      else
        code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
        response.response_code == code
      end
    else
      response.response_code == type
    end
  end

  failure_message do |response|
    message or
      "Expected response to be a <#{type}>, but was <#{response.response_code}>"
  end
end
Run Code Online (Sandbox Code Playgroud)

更新:2014-07-02

现在可以使用RSpec Rails 3开箱即用:https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/matchers/have-http-status-matcher


apn*_*ing 11

一方面,响应是通过以下方法构建的:

  • 成功?

  • 重定向?

  • 不能处理的?

  • 完整清单: response.methods.grep(/\?/)

另一方面,Rspec谓词将每个foo?方法转换为be_foo匹配器.

不幸的是,不确定你是否能以这种方式获得201,但创建自定义匹配器非常容易.

注意Rails测试仅依赖于一些状态.


sch*_*jos 9

使用rspec-rails(从rspec 3开始),可以使用它

expect(response).to have_http_status(:created)
Run Code Online (Sandbox Code Playgroud)

更新2018-06-11:

如导轨6,一些匹配器的将被替换(例如,success通过successful).