2012-07-12 3 views
9

나는 액션 코드를 다음 있습니다 : 이것은 브라우저에서 잘 작동Rspec 2에서 렌더링을 테스트하는 방법 : file => 'public/404.html'?

render :file => 'public/404.html' and return 

.

it "renders 404" do 
    get :new 
    response.should render_template('public/404.html') 
    end 

다음과 같은 오류이 예의 결과를 실행 : :이 테스트하려면 다음 RSpec에 예를 작성했습니다

Failure/Error: response.should render_template('public/404.html') 
    Expected block to return true value. 

또한 오류가 response.should render_template(:file => 'public/404.html')하지만 너무 결과를 시도했습니다.

어떻게 테스트해야합니까?

답변

17

당신은 사용해야합니다 :이 답변을 살펴 가지고는 다른 페이지입니다

response.should render_template(:file => "#{Rails.root}/public/404.html") 
+6

이 작동하지 않습니다 : render_template (: file => "bogus file") 이 통과합니다. – therin

+0

나를 위해 작동합니다 RSpec 3.2, Rails 4.2 – fny

5

404 페이지를 표시하려면 상태 코드를 설정해야합니다. 이것은 public/404를 직접 렌더링하는 대신에 it { should respond_with :not_found }

대신에 테스트 할 것입니다. 더 나은 방법이 있습니다. How to redirect to a 404 in Rails?

+0

어떤 경우를 구체적으로 404.html (또는 502.html)하지? 예를 들어 일부 매개 변수에 따라 다른 방문 페이지를 다른 사용자에게 보여주고 싶습니다. – Chandranshu

+0

나는 이것을 라우터에 넣고 라우팅 테스트를 작성합니다. – phoet

관련 문제