2014-07-16 72 views
2

这里是我的测试:Rspec的/ Rails的控制器测试404不工作

describe "GET show" do 

    it "assigns service_request as @service_request" do 
     get :show, { company_id: @company.id, id: service_request.id } 
     expect(assigns(:service_request)).to eq service_request 
    end 

    it "returns 404 when service_request is not found" do 
     get :show, { company_id: @company.id, id: "foo" } 
     expect(response.status).to eq 404 
    end 

    end 

在我的终端的错误是:

1) ServiceRequestsController GET show returns 404 when service_request is not found 
    Failure/Error: get :show, { company_id: @company.id, id: "foo" } 
    ActiveRecord::RecordNotFound: 
     Couldn't find ServiceRequest with 'id'=foo [WHERE (company_id IS NOT NULL)] 
    # ./spec/controllers/service_requests_controller_spec.rb:44:in `block (3 levels) in <top (required)>' 
    # -e:1:in `<main>' 

显然,这是不正确的,但我不知道有什么问题

回答

3

Rails抛出一个ActiveRecord :: RecordNotFound错误,而不是重定向到一般的404页面。您需要在控制器中使用rescue_from处理该错误,并重定向到状态为404的404视图。

+0

啊,呃!让我看看,谢谢! – dennismonsewicz

+2

纠正我,如果我错了,但它会返回404状态,如果这是一个'请求'规格权?由于它是一个控制器级别规范,因此它不会遍历整个堆栈到异常应用程序。 –

+0

@LeoCorrea可能是这样,但我不是100%确定。但是我知道是什么原因导致了他的错误,因为我不久前也遇到了同样的问题。 – jkeuhlen