【发布时间】:2021-08-04 12:43:47
【问题描述】:
我正在编写测试来检查某些端点是否返回 200 状态。
RSpec.describe 'API -> ' do
before do
@token = get_token
end
describe 'Status of below end points should be 200 -->' do
it "/one should return a status code of 200" do
get("/api/v1/one", params: {
authentication_token: @token
})
expect(response.status).to eql(200)
end
it "/two should return a status code of 200" do
get("/api/v1/two", params: {
authentication_token: @token
})
expect(response.status).to eql(200)
end
it "/three should return a status code of 200" do
get("/api/v1/three", params: {
authentication_token: @token
})
expect(response.status).to eql(200)
end
end
end
有很多这样的端点,我想知道是否有更有效的方法来写这个,比如
RSpec.describe 'API -> ' do
before do
@token = get_token
@end_points = ['one', 'two', 'three', 'four', 'five']
end
describe 'Status of below end points should be 200 -->' do
@end_points.each do |end_point|
it "/#{end_point} shold returns a status code of 200" do
get("/api/v1/#{end_point}", params: {
authentication_token: @token
})
expect(response.status).to eql(200)
end
end
end
end
但这不起作用并给出错误each called for nil。
对此的任何帮助都会很棒,谢谢。
【问题讨论】:
标签: ruby ruby-on-rails-5 rspec-rails