I'm roughly following the Rails Tutorial by Michael Hartl, right now I'm trying to set up a few RSpec tests. This is my routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
end
I have a spec/integration/site_layout_spec.rb
file in which I can use helper methods such as signup_path
:
# site_layout_spec.rb
require 'rails_helper'
RSpec.describe 'Root' do
it 'shows the home page' do
get signup_path # just to show it's working
get root_path
expect(root_path).to render_template('static_pages/home')
end
end
I also have a file spec/controllers/users_controller_spec.rb
. In this file, none of the *_path
helper methods seems to work.
# user_controller_spec.rb
require 'rails_helper'
RSpec.describe 'User signup page' do
it 'should be available' do
get signup_path
expect(signup_path).to exist
end
end
When running rspec
, I get
Failure/Error: get signup_path
NoMethodError: undefined method `signup_path' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:5:in `block (2 levels) in <main>'
Why don't the helpers work in my second test?
The latter is a controller spec, there you're expected to test controller without routing, by calling it's actions:
get :new