我遵循Ruby on Rails教程并遇到了以下问题:
me @ me:〜/ Ruby / sample_app $ bundle exec rspec spec / requests / static_pages_spec.rb
˚F
故障:
1)静态页面主页应该包含内容' Sample App' 失败/错误:访问' / static_pages / home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x00000001e1df88>
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
以0.0006秒结束(文件加载0.08149秒) 1例,1次失败
失败的例子:
rspec ./spec/requests/static_pages_spec.rb:7#静态页面主页应该有内容&#39;示例应用程序&#39;
这是我的Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'rspec-rails'
end
group :assets do
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
end
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
group :test do
gem 'capybara'
end
group :prodcution do
gem 'pg'
end
我的规范/要求/ static_pages_spec.rb:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
在线挖掘建议在spec / spec_helper.rb中输入config.include Capybara :: DSL。我做到了,并没有奏效。另一个建议说我需要将static_pages_spec.rb移动到spec / features。我做到了,那也没有用。
帮助? :d
答案 0 :(得分:0)
visit
是功能规范中使用的方法。
在您的请求规范中,您使用HTTP谓词get
,post
,patch
等来发送http请求。
require 'rails_helper'
RSpec.describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
get '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
我相信您使用的是过时的教程以及subject.should
已经折旧很长一段时间了
expect(subject).to
。
虽然您可以将capybara混合到您的请求规范中,但最好将请求规范用作低级集成测试,并将更高级别的点击和按钮推送到您的功能规范。
当您遇到麻烦时,您可能需要参考官方的RSpec-rails文档。