使用Rspec和Capybara设置新的RoR项目

时间:2012-07-08 08:37:55

标签: ruby-on-rails ruby ruby-on-rails-3 rspec capybara

我正在尝试学习this tutorial之后的RoR,我目前正在第3章。如果我逐行跟进它,那么教程工作正常。但是,本教程中使用的命令会禁止生成默认测试。当我试图保留它们并可能在我的项目中使用它们时,我总是在某个地方撞墙。

你能告诉我我做错了吗?

$ 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文件,以测试capybara是否有效:

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)>'

尚未找到AFAIK属于Capybara的visit方法。取消注释home.html.erb_spec.rb中的三个额外要求不会改变结果中的任何内容。

任何想法我做错了什么?或者我应该做得更好?

Rails版本:3.2.6

1 个答案:

答案 0 :(得分:3)

将测试放在requests目录而不是views