0

按照在线 Hartl 教程,似乎无法找到第 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”,因为“visit”是一种 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为测试制定有效的方法需要什么?

4

3 回答 3

1

您实际上拼错了“访问”一词:

before { vist root_path }

为了使测试通过,您需要将AboutHelp测试的路径更改为各自的路径。

例如,您正在测试帮助页面,但不是访问help_path,而是访问root_path

describe "Help page" do
  before { visit root_path }

对于About page.

于 2013-08-18T21:01:14.673 回答
0

我有同样的情况,但我的路还不错,测试失败了

“规范/支持/实用程序.rb”

def full_title(page_title)
    base_title = "agroBox App"
    if page_title.empty?
        base_title
    else
        "#{base_title} | #{page_title}"
    end
end

“规范/请求/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)>'
于 2014-04-23T07:49:37.600 回答
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
于 2014-04-23T08:42:45.920 回答