我正在尝试学习RoR跟在this tutorial之后,我目前在第3章。如果逐行跟随它,教程会正常工作。但是,本教程中使用的命令会抑制默认测试的生成。当我试图保留它们并可能在我的项目中使用它们时,我总是在某个地方撞墙。使用Rspec和Capybara建立一个新的RoR项目
你能告诉我我做错了什么吗?
$ rails new myproject
$ cd myproject/
$ echo "gem 'rspec'" >> Gemfile
$ echo "gem 'rspec-rails'" >> Gemfile
$ echo "gem 'capybara'" >> Gemfile
$ bundle install
$ bundle --binstubs
$ rails generate rspec:install
$ rails generate controller StaticPages home help about
然后我编辑spec/views/static_pages/home.html.erb_spec.rb
文件,以测试是否水豚工作:
require 'spec_helper'
#require 'capybara'
#require 'capybara/rails'
#require 'capybara/rspec'
describe "static_pages/home.html.erb" do
it 'should have a right title' do
visit '/static_pages/home'
page.should have_selector('title', :text => 'Home')
end
end
运行bin/rspec
在这一点上,显然,最终以失败。那么,可能会有失败。这些失败的一个原因是更为惊人,虽然:
1) static_pages/home.html.erb should have a right title
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_5:0x00000003dfd268>
# ./spec/views/static_pages/home.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'
的visit
方法,其中AFAIK是水豚的一部分,一直没有找到。取消注释home.html.erb_spec.rb
中的三个额外要求不会改变结果中的任何内容。
任何想法我做错了什么?或者我应该做的更好?
Rails的版本:3.2.6
如果将'require capybara'移动到spec_helper文件中会发生什么? – ScottJShea 2012-07-08 09:31:42
@ScottJShea同样,我害怕。 – kamilk 2012-07-08 09:56:51