我有一个Rails 5应用程序,用于创建和销毁" csstests。"我正在尝试为控制器销毁操作编写测试。这是行动:
def destroy
@test = Csstest.find(params[:id])
if @test.destroy
flash.now[:success] = "Your test was removed"
redirect_to csstests_path
end
end
这是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
这是我从失败中得到的信息:
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
答案 0 :(得分:0)
阅读HTTP codes的基础知识 - 特别是:
2xx
回复是&#34;成功&#34;状态代码。3xx
回复是&#34;重定向&#34;状态代码。在您的控制器中,如果删除了Csstest
记录,则您选择执行重定向:redirect_to csstests_path
。
如果这确实是您想要的行为,那么您可以更新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)
还可以明确返回重定向的301
或303
状态代码(请参阅the documentation)。但同样,这些将不会被视为&#34;成功&#34;码。