【发布时间】:2020-10-30 02:05:03
【问题描述】:
所以我在让一些 RSpec 测试失败时遇到了一些麻烦。无论我尝试什么,它们总是通过,嗯,其中一个测试工作正常,我验证如果我修改它正在测试的代码,它可能会失败。
我正在尝试测试对外部 API 做出的 JSON 响应的内容,以确保我的控制器正确排序这些返回的 JSON 对象。
我在这里遗漏了什么吗?为什么我不能让这些失败?
RSpec.describe 'Posts', type: :request do
describe 'ping' do
it 'returns status 200' do # This text block works correctly.
get "/api/ping"
expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response).to have_http_status(200)
end
end
describe 'get /api/posts' do # all tests below always pass, no matter what.
it 'should return an error json if no tag is given' do
get "/api/posts"
expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response.body).to eq("{\"error\":\"The tag parameter is required\"}")
end
it 'should return a json of posts' do
get "/api/posts?tags=tech" do
expect(body_as_json.keys).to match_array(["id", "authorid", "likes", "popularity", "reads", "tags"])
end
end
it 'should sort the posts by id, in ascending order when no sort order is specified' do
get "/api/posts?tags=tech" do
expect(JSON.parse(response.body['posts'][0]['id'].value)).to_be(1)
expect(JSON.parse(response.body['posts'][-1]['id'].value)).to_be(99)
end
end
it 'should sort the posts by id, in descending order when a descending order is specified' do
get "/api/posts?tags=tech&direction=desc" do
expect(JSON.parse(response.body['posts'][0]['id'].value)).to_be(99)
expect(JSON.parse(response.body['posts'][-1]['id'].value)).to_be(1)
end
end
end
在“如果没有给出标签,应该返回错误 json”的 get 块中,我什至尝试了 expect(4).to eq(5),甚至 THIS 都通过了!
在这里非常感谢您的帮助!
【问题讨论】:
标签: ruby-on-rails testing rspec