如何在RSpec中使用HTTP状态代码符号? [英] How to use HTTP status code symbols in RSpec?
本文介绍了如何在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 ==)
推荐答案
一方面,使用以下方法构建响应:
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屋!
查看全文