遵循Hartl Tutorial在线并且似乎无法找到第5章第3.0节的解决方案。在本教程的这一点上,我似乎应该通过在“spec / support / utilities.rb”中定义full_title然后编辑“spec / requests / static_pages_spec.rb”文件来使我的rspec代码更简洁。
这是我的“spec / support / utilities.rb”:
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
和我的“spec / requests / static_pages_spec.rb”:
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path }
it { should have_content('Sample App') }
it { should have_title(full_title('')) }
it { should_not have_title('| Home') }
end
describe "Help page" do
before { vist root_path }
it { should have_content('Help') }
it { should have_title(full_title('Help')) }
end
describe "About page" do
before { visit root_path}
it { should have_content('About') }
it { should have_title(full_title('About')) }
end
describe "Contact page" do
before { visit contact_path }
it { should have_content('Contact') }
it { should have_title(full_title('Contact')) }
end
end
和我的“spec_helper.rb”:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
当我跑步时:
$ bundle exec rspec spec/requests/static_pages_spec.rb
我明白了:
Failures:
1) Static pages About page
Failure/Error: it { should have_title(full_title('About')) }
expected #has_title?("Ruby on Rails Tutorial Sample App | About") to return true, got false
# ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top (required)>'
2) Static pages Help page
Failure/Error: before { vist root_path }
NoMethodError:
undefined method `vist' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fe53b33f338>
# ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
3) Static pages Help page
Failure/Error: before { vist root_path }
NoMethodError:
undefined method `vist' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fe53b44d8b0>
# ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
Finished in 0.28441 seconds
9 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages About page
rspec ./spec/requests/static_pages_spec.rb:18 # Static pages Help page
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Help page
我在其他帖子上看到这个错误可能是由于缺少“Capybara”,因为“访问”是一种Capybara方法。我检查了我的Gem文件,它显示了Capybara:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.7'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup.
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', github: 'sporkrb/spork-rails'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.0'
gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
# Uncomment this line on OS X.
# gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.2'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
我不确定使visit
成为测试的有效方法需要什么?
答案 0 :(得分:1)
你实际拼错了访问这个词:
before { vist root_path }
要通过测试,您需要将关于和帮助测试的路径更改为各自的路径。
例如,您正在测试帮助页面,但您没有访问help_path
,而是访问了root_path
describe "Help page" do
before { visit root_path }
同样适用于About page
。
答案 1 :(得分:0)
我有相同的情况,但我的路径是okey,测试失败
<强>线 “spec /支持/ utilities.rb”强>
def full_title(page_title)
base_title = "agroBox App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
<强>线 “spec /请求/ static_pages_spec.rb”强>
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path }
it { should have_content('agroBox App') }
it { should have_title(full_title('')) }
it { should_not have_title('| Home') }
end
describe "Help page" do
before { visit help_path }
it { should have_content('Help') }
it { should have_title(full_title('Help')) }
end
describe "About page" do
before { visit about_path }
it { should have_content('About') }
it { should have_title(full_title('About Us')) }
end
describe "Contact page" do
before { visit contact_path }
it { should have_content('Contact') }
it { should have_title(full_title('Contact')) }
end
end
<强> “spec_helper.rb”强>
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
$ bundle exec rspec spec/requests/static_pages_spec.rb
Failures:
1) Static pages About page should have title "agroBox App | About Us"
Failure/Error: it { should have_title(full_title('About Us')) }
expected #has_title?("agroBox App | About Us") to return true, got false
# ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top (required)>'
2) Static pages Contact page should have title "agroBox App | Contact"
Failure/Error: it { should have_title(full_title('Contact')) }
expected #has_title?("agroBox App | Contact") to return true, got false
# ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>'
3) Static pages Help page should have title "agroBox App | Help"
Failure/Error: it { should have_title(full_title('Help')) }
expected #has_title?("agroBox App | Help") to return true, got false
# ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>'
答案 2 :(得分:0)
解决!也许它帮助了所有关于Capybara 2.x的错误,只是像星号一样的变化,你的测试将通过;)
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path }
it { should have_content('some App') }
it { should have_title(full_title('')) }
it { should_not have_title('| Home') }
end
describe "Help page" do
before { visit help_path }
**it { page.should have_content('Help') }
it { page.has_title?(full_title('Help')) }**
end
describe "About page" do
before { visit about_path }
**it { page.should have_content('About') }
it { page.has_title?(full_title('About Us')) }**
end
describe "Contact page" do
before { visit contact_path }
**it { page.should have_content('Contact') }
it { page.has_title?(full_title('Contact')) }**
end
end