Reputation: 18865
Rails newb here.
Trying to RSpec test a 200 status code for an index route.
In my index_controller_spec.rb:
require 'spec_helper'
describe IndexController do
it "should return a 200 status code" do
get root_path
response.status.should be(200)
end
end
routes.rb:
Tat::Application.routes.draw do
root to: "index#page"
end
index_controller:
class IndexController < ApplicationController
def page
end
end
When I visit on my browser all is fine but RSpec command line gives an error:
IndexController should return a 200 status code
Failure/Error: get '/'
ActionController::RoutingError:
No route matches {:controller=>"index", :action=>"/"}
# ./spec/controllers/index_controller_spec.rb:6:in `block (2 levels) in <top (required)>
'
I don't understand?!
Thanks.
Upvotes: 1
Views: 680
Reputation: 8546
Welcome to the Rails world! Testing comes in many different flavors. It appears that you're confusing a controller test with a routing test.
You're seeing this error because root_path
is returning /
. The get :action
within an RSpec controller test is meant to call that method on that controller.
If you notice your error message, it says :action => '/'
To test your controller, change your test to:
require 'spec_helper'
describe IndexController do
it "should return a 200 status code" do
get :page
response.status.should be(200)
end
end
If you're interested in a routing test, see https://www.relishapp.com/rspec/rspec-rails/docs/routing-specs An example would be:
{ :get => "/" }.
should route_to(
:controller => "index",
:action => "page"
)
Upvotes: 3