rspec test: failure/error: visit ‘/home’ no method error or routing error: no route matches [get] “/home”

I’m developing a Ruby on Rails web app, and this problem tripped me up for a while. I couldn’t get an rspec test to work.

Here are the errors I was getting:

emlmbp:captivesurveys eml$ rake spec:views
/Users/eml/.rbenv/versions/1.9.3-p194/bin/ruby -S rspec ./spec/views/home/index.html.erb_spec.rb
FF

Failures:

1) home/index.html.erb displays proper title
Failure/Error: visit ‘/’
NoMethodError:
undefined method `visit’ for #<RSpec::Core::ExampleGroup::Nested_1:0x007fc1ab87d920>
# ./spec/views/home/index.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>’

2) home/index.html.erb displays copyright text
Failure/Error: visit ‘/’
NoMethodError:
undefined method `visit’ for #<RSpec::Core::ExampleGroup::Nested_1:0x007fc1ac461520>
# ./spec/views/home/index.html.erb_spec.rb:9:in `block (2 levels) in <top (required)>’

Finished in 0.01271 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/views/home/index.html.erb_spec.rb:4 # home/index.html.erb displays proper title
rspec ./spec/views/home/index.html.erb_spec.rb:8 # home/index.html.erb displays copyright text

Randomized with seed 51604

rake aborted!
/Users/eml/.rbenv/versions/1.9.3-p194/bin/ruby -S rspec ./spec/views/home/index.html.erb_spec.rb failed

Tasks: TOP => spec:views
(See full trace by running task with –trace)

 

Turns out the solution isn’t too difficult. First, make sure your Gemfile includes capybara. Run bundle install –without production to load the Gemfile. Then, put your spec files in a directory named “requests” instead of “views”.

Example:

emlmbp:captivesurveys eml$ rake spec:requests
No examples matching ./spec/requests/**/*_spec.rb could be found
emlmbp:captivesurveys eml$ mv ./spec/views ./spec/requests
emlmbp:captivesurveys eml$ rake spec:requests
/Users/eml/.rbenv/versions/1.9.3-p194/bin/ruby -S rspec ./spec/requests/home/index.html.erb_spec.rb
..

Finished in 0.13965 seconds
2 examples, 0 failures

Randomized with seed 58185

 

Bonus tip: In vim / mvim / MacVim, Ctrl+w+h to jump to the left window. Ctrl+w+l to jump to the right window.

Bonus tip #1.5: Revert to the last saved version of the current file with :e!

Bonus tip #1.6: Here’s some info on using <leader> in vim.

Map NERDTree to something like <Leader>nt. Or even just <Leader>n. Or toggle it with F12.

If you have difficulty installing NERDTree with pathogen.vim, you can manually copy the NERD_tree.vim file into your ~/.vim/plugin directory.

Example .gvimrc

Bonus tip #2: You can get the latest version of git with Homebrew: brew install git

Use brew doctor to see if you need to modify your PATH.

You may need to upgrade git if the git gui command is not available you.

Here’s a highly recommended Ruby on Rails tutorial.

Leave a Reply