I have a really simple controller action I'd like to test.
it "should render 404 page if template does not exist" do
get 'show', :page => "does_not_exist"
response.should render_template("/public/404")
end
Basically I want to have an action that dynamically renders a static page. That way I don't have to have N actions for N pages.
The implementation for this test is as follows:
def show
begin
render(params[:page])
rescue ActionView::MissingTemplate
render("/public/404")
end
end
This code actually works, but the test fails anyway. It doesn't make the most sense to me, because the test reports that the exception is getting thrown... even though the controller is supposed to catch it:
ActionView::MissingTemplate: Missing template webpages/does_not_exist with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/egervari/Projects/training/app/views", "/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.3.4/app/views"
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_view/paths.rb:15:in `find'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_view/lo开发者_StackOverflowokup_context.rb:81:in `find'
Is the exception getting thrown after the controller method exits or something like that? How can I get my test to pass?
Thanks!
EDIT: It turns out I did nothing wrong. The problem was because I had spork running and I needed to restart it. I am starting to think that Spork is more trouble than it's worth. I am new to ruby and rails, so it's hard for me to tell what is a legitimate error and when it's due to spork. Maybe I shouldn't use spork until next month or something.
You should dump the result.body:
require 'pp'
pp response.body
Then check this against your assumptions, you are expecting the 404 page, but you may be getting the Rails error page which it renders for you in development mode.
ian.
See relevant question: Basic Rails 404 Error Page
精彩评论