[英]Destroy action in RSpec test not returning success
I have a Rails 5 application that creates and destroys "csstests." 我有一个创建和销毁“ csstests”的Rails 5应用程序。 I am attempting to write a test for the controllers destroy action. 我正在尝试为控制器销毁动作编写测试。 Here is the action: 这是动作:
def destroy
@test = Csstest.find(params[:id])
if @test.destroy
flash.now[:success] = "Your test was removed"
redirect_to csstests_path
end
end
Here is the RSpec test: 这是RSpec测试:
describe 'DELETE #destroy' do
before do
test1, test2 = Csstest.create, Csstest.create
delete :destroy, params: { id: test1.id }
end
it 'returns HTTP success' do
expect(response).to be_success
expect(response).to return_http_status(200)
end
end
This is the message I get from the failure: 这是我从失败中得到的消息:
1) CsstestsController DELETE #destroy returns HTTP success
Failure/Error: expect(response).to be_success
expected `#<ActionDispatch::TestResponse:0x007fb1362beb68 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu...spatch::Http::Headers:0x007fb1362de4b8 @req=#<ActionController::TestRequest:0x007fb1362becf8 ...>>>>.success?` to return true, got false
Read up on the basics of HTTP codes - in particular: 阅读HTTP代码的基础知识-特别是:
2xx
responses are "success" status codes. 2xx
响应是“成功”状态代码。 3xx
responses are "redirection" status codes. 3xx
响应是“重定向”状态代码。 In your controller, you have chosen to perform a redirect if the Csstest
record is deleted: redirect_to csstests_path
. 在控制器中,如果删除了Csstest
记录,则选择执行重定向 : redirect_to csstests_path
。
If this is truly the desired behaviour, then you could update your rspec
test to expect this: 如果这确实是期望的行为,则可以更新rspec
测试以期望达到以下目的:
expect(response).to redirect_to(csstests_path)
# This second assertion is probably not really necessary
# But I'll leave it here for completion...
expect(response).to return_http_status(302)
It is also possible to explicitly return 301
or 303
status codes for the redirect (see the documentation ). 还可以显式返回用于重定向的301
或303
状态代码(请参见文档 )。 But again, these will not be considered "success" codes. 但是同样,这些将不被视为“成功”代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.