Ruby on Rails,测试时出现 Rspec 错误 [英] Ruby on Rails , Rspec Error while testing

查看:33
本文介绍了Ruby on Rails,测试时出现 Rspec 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am following the tutorial from Michael Hartl . I tried the first test example for testing the app with Rspec and when I execute this command "bundle exec rspec spec\requests\static_pages_spec.rb" I get this error.

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

Gemfile.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

推荐答案

您需要在您的规范源中require 'spec_helper'.

You need to require 'spec_helper' in your spec source.

你的 spec_helper 应该同时包含 rspec/railscapybara/rails.

Your spec_helper should include both rspec/rails and capybara/rails in it.

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

You'll want to use get instead of visit if you want to access the response, however.

这篇关于Ruby on Rails,测试时出现 Rspec 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆