12

I have following code in an action:

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

This works fine in the browser. I have written the following rspec example to test this:

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

Running this example results in the following error:

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

I have also tried response.should render_template(:file => 'public/404.html') but that too results in an error.

How should I test this?

Chandranshu
  • 3,669
  • 3
  • 20
  • 37

2 Answers2

20

You should use:

response.should render_template(:file => "#{Rails.root}/public/404.html")
Kamil Bednarz
  • 748
  • 6
  • 9
5

if you want to show a 404 page, remember to set the status code. this is also what i would test: it { should respond_with :not_found }

instead of rendering the public/404 directly, there are better ways of doing this. have a look at this answer: How to redirect to a 404 in Rails?

Community
  • 1
  • 1
phoet
  • 18,688
  • 4
  • 46
  • 74
  • What if it is any other page and not 404.html (or 502.html) specifically? For example, depending on some parameter, I want to show different landing pages to different users. – Chandranshu Jul 12 '12 at 13:58
  • i would put this in the router and write routing tests for it. – phoet Jul 12 '12 at 15:28