2

我正在关注 Michael Hartl 的教程。我尝试使用 Rspec 测试应用程序的第一个测试示例,当我执行此命令“bundle exec rspec spec\requests\static_pages_spec.rb”时,我收到此错误。

F

Failures:

  1) Home page should have the content 'Sample App'
     Failure/Error: visit '/static_pages/home'
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x39e1510 @example=nil>
     # ./spec/requests/static_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:3 # Home page should have the content 'Sample App'

static_pages_spec.rb

describe "Home page" do
  it "should have the content 'Sample App'" do
    visit '/static_pages/home'
    page.should have_content('Sample App')
 end
end

宝石文件.rb

source 'https://rubygems.org'

gem 'rails', '3.2.1'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.10.0'
end

group :assets do
  gem 'sass-rails', '3.2.4'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails'

group :test do
  gem 'capybara', '1.1.2'
end

group :production do
  gem 'pg', '0.12.2'
end
4

2 回答 2

4

你需要require 'spec_helper'在你的规范源中。

spec_helper应该包括rspec/rails两者capybara/rails

但是,如果您想访问响应,您将需要使用get而不是。visit

于 2012-09-04T00:44:43.170 回答
1

如果static_pages_spec.rb有字符串require 'spec_helper'并且你得到

失败/错误:访问“/static_pages/home”

  • 添加到spec_helper.rb字符串config.include Capybara::DSL

它帮助了我

于 2014-02-12T17:28:29.913 回答