问题
I'm currently going through the Ruby on Rails Tutorial by Michael Hartl, and in Section 3.2.2 of the tutorial, he talks about using RSpec to test for the titles of the page. My tests (which I wrote myself, following his tutorial) kept failing the title tests (it can find the tag, but the title is always a blank string), and some searching on Google told me I needed to include the function render_views
to pass the tests. My problem is that no matter what I try, RSpec returns a NoMethodError
for render_views
. The things I've tried:
config.render_views
inspec_helper.rb
describe "Static Pages" do render_views ... end
in the spec filedescribe "Static Pages" do RSpec::Rails::ViewRendering.render_views ... end
in the spec file
All of them return a NoMethodError
.
I'm not following his Gemfile exactly, so the versions of Ruby and the relevant gems are:
- Ruby: 1.9.3-p125
- rails: 3.2.9
- rspec: 2.12.0
- rspec-rails: 2.12.0
- capycabra: 2.0.1
I'm working in RubyMine 4.5.4, on Windows 7. Running the test in a command prompt returns the same errors however. The spec file is static_pages_spec.rb
and is located in spec/requests
The code in my static_pages_spec.rb
:
require_relative '../spec_helper'
describe "Static Pages" do
#RSpec::Rails::ViewRendering.render_views, returns NoMethodError
#render_views, returns NameError
describe "Home Page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => "Sample App")
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help Page" do
it "should have the h1 'Help'" do
visit '/static_pages/help'
page.should have_selector('h1', :text => "Help")
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About Page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => "About Us")
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us")
end
end
end
My spec_helper.rb
is the spec_helper.rb
automatically generated by creating running rails generate integration_test
after creating the project with --no-test-framework
, with these lines added in the config
block:
# Fix NoMethodError for method 'visit'
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
EDIT
After some testing, I've managed to get the tests to pass by changing the capybara version to 1.1.2, while still using rspec 2.12.0 and rspec-rails 2.12.0 and not using render_views. While I'm glad the tests are passing, it still doesn't solve the problem of the missing render_views
method, which still crops up if I try to use it.
回答1:
Well, after a bit more testing, the mystery of the missing render_views
method is solved. To include the render views method, apparently config.include RSpec::Rails::ViewRendering
in the spec_helper
is needed before calling config.render_views
in the spec_helper
or render_views
in the spec file.
Secondly, with regards to the tests passing or failing, the problem seems to be in Capybara itself. Apparently, it seems that the way the visit
method is implemented in 1.1.2 and 2.0.1 is significantly different in what it returns; even after including render_views
, the test still fails (without using config.include Capybara::RSpecMatchers
). Using it seems to provide an insight into its implementation though; the failure message from Capybara on using the RSpecMatchers
is that it is looking for CSS instead of HTML. I don't claim to fully understand why it fails, however, so if anyone can enlighten me as to why Capybara 2.0.1 causes the tests to fail while 1.1.2 allows the tests to pass, that would be great.
回答2:
Capybara 2.0 requires all tests to be in spec/features, and not in spec/requests.
You can read all about it here.
I would suggest that you stick to the versions mentioned in Hartl's tutorial, atleast in your beginner days. Will help you get through the tutorial faster!
来源:https://stackoverflow.com/questions/13840602/render-views-returning-nomethoderror