Reputation: 149
I'm trying to write an rspec test for a custom redirect page in Rails. Basically I've got a wildcard match in routes.rb that gets a page name, and a show method in a "Pages" controller that checks if a partial by that name exists. If it doesn't find a matching partial, it renders a 404 page and sets the status to 404. All this works in the browser, however rspec sees it as a 200 "OK" response, not a 404.
The code: Pages controller show method (partial_exists? is a private method that I've thoroughly tested, and I can verify using the test logs that _missing.html.erb is being rendered as expected when rspec runs)
def show
@page_name = params[:page_name].to_s.gsub(/\W/,'')
unless partial_exists?(@page_name)
render :partial => 'missing', :status => :not_found
end
end
routes.rb:
match '/' => 'pages#show', :page_name => 'index'
match '*page_name' => 'pages#show'
spec:
require 'spec_helper'
describe PagesController do
render_views
describe "get page name of request" do
it "should generate http success for a defined partial" do
visit '/'
response.should be_success
end
it "should give respond with the 404 missing page for an undefined partial" do
visit '/blahblahblah'
response.status.should be(404)
end
end
end
The first test runs as expected, and renders _index.html.erb partial with a status code of 200. The second test renders _missing.html.erb (I've verified this by looking at the test logs), but reports a status code of 200, not 404 as expected. When I run it in the browser, it renders _missing.html.erb with a status code 404.
Any ideas?
Upvotes: 0
Views: 743
Reputation: 47548
It appears that you are confusing request (feature) specs with controller specs. visit
is for use in request specs. For a controller spec, you want something like this:
describe "get page name of request" do
it "should generate http success for a defined partial" do
get :index
response.should be_success
end
end
Use get
or post
along with the appropriate action name to test the corresponding action in the controller.
Upvotes: 2