2013-08-18 34 views
0

在線學習Hartl教程後,似乎無法找到第5章第3.0節的解決方案。在本教程的這一點,我似乎應該通過在「spec/support/utilities.rb」中定義full_title然後編輯「spec/requests/static_pages_spec.rb」文件來讓我的rspec代碼更加簡潔。Rails教程5.3.0 TDD錯誤:「未定義方法訪問」

這裏是我的 「投機/支持/ 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 

和我的 「投機/請求/ 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 

我已經看到了其他職位,這種錯誤可能是由於缺乏「水豚」,因爲「訪問」是一個水豚方法。我檢查了我的寶石文件,並將其顯示水豚:

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用於測試的有效方法是什麼?

回答

1

您實際上已經拼錯了單詞,請訪問:

before { vist root_path }

,並在測試通過,您需要將關於幫助測試的路徑更改爲各自的路徑。

例如,如果正在測試的幫助頁面,但不是逛help_path,您正在訪問的root_path

describe "Help page" do 
    before { visit root_path } 

也是一樣的About page

+0

Thanks!我在「describe」幫助頁面「do」下更正了「visit」。但是,我不確定你的意思是將_About_和_Help_測試的路徑更改爲各自的路徑。對不起,我對編程很陌生。 – kyu1012

+0

@ kyu1012我剛剛編輯了我的答案。 – PericlesTheo

+0

啊我現在明白了。我會盡快進行更改,並讓您知道發生了什麼。謝謝! – kyu1012

0

我有同樣的情況,但我的路的是好和測試失敗

「規格/支持/ utilities.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)>' 
0

解決了!也許它可以幫助某人所有關於水豚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 
相關問題