如何在 RSpec 中使用 HTTP 状态代码符号? [英] How to use HTTP status code symbols in RSpec?

查看:15
本文介绍了如何在 RSpec 中使用 HTTP 状态代码符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

render json: {
    auth_token: user.authentication_token, 
    user: user
  }, 
  status: :created

render json: {
    errors: ["Missing parameter."]
  }, 
  success: false, 
  status: :unprocessable_entity

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

In the code of my request spec I also would like to use the symbols:

post user_session_path, email: @user.email, password: @user.password
expect(last_response.status).to eq(201)

...

expect(last_response.status).to eq(422)

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

However each test where I use the symbols instead of integers fails:

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

  expected: :created
       got: 201

  (compared using ==)

<小时>

这里是最新的 HTTP 状态码符号列表在机架中.

推荐答案

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

On the one hand, response is built with methods like:

  • 成功了吗?

  • success?

重定向?

无法处理?

完整列表:response.methods.grep(/?/)

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

On the other hand, Rspec predicates transforms every foo? method to a be_foo matcher.

不幸的是,不确定您可以通过这种方式使用 201,但创建自定义匹配器非常容易.

Not sure you can have the 201 this way unfortunately, but creating a custom matcher is quite easy.

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

Note Rails test only rely on a few statuses.

这篇关于如何在 RSpec 中使用 HTTP 状态代码符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆