質問

Following the Hartl Tutorial online and can't seem to find a solution to Chapter 5 section 3.0. At this point of the tutorial, I seems that I am supposed to make my rspec code more concise by defining a full_title in "spec/support/utilities.rb" then editing the "spec/requests/static_pages_spec.rb" file.

Here is my "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

and my "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

and my "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

When I run:

$ bundle exec rspec spec/requests/static_pages_spec.rb

I get:

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 

I've seen on other posts that this error may be due to a lack of "Capybara" because "visit" is a Capybara method. I checked my Gem file and it shows 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

I'm not sure what is needed to make visit a valid method for the test?

役に立ちましたか?

解決

You have actually misspelled the word visit:

before { vist root_path }

And for the test to pass, you need to change the path of the About and Help tests to their respective paths.

For example, you are testing the Help page but instead of visiting the help_path, you are visiting the root_path

describe "Help page" do
  before { visit root_path }

Same goes for the About page.

他のヒント

i have same situation but my path's is okey and tests are failed

"spec/support/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/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('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)>'

Solved! Maybe it helps someone all this errors about Capybara 2.x, just a change like in lines with asterisks and your tests will pass ;)

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top