2012-06-01 8 views
0

В настоящее время я работаю через Хартл. В главе 5 я буду добавлять код Hartl от листинга 5.27 (ниже) на мою спецификации/запросы/static_pages_spec.rbHartl Глава 5. Раздел 5.27

require 'spec_helper' 

describe "Static pages" do 

subject { page } 

describe "Home page" do 
before { visit root_path } 

it { should have_selector('h1', text: 'Sample App') } 
it { should have_selector('title', text: full_title('')) } 
it { should_not have_selector 'title', text: '| Home' } 
end 

describe "Help page" do 
before { visit help_path } 

it { should have_selector('h1', text: 'Help') } 
it { should have_selector('title', text: full_title('Help')) } 
end 

describe "About page" do 
before { visit about_path } 

it { should have_selector('h1', text: 'About') } 
it { should have_selector('title', text: full_title('About Us')) } 
end 

describe "Contact page" do 
before { visit contact_path } 

it { should have_selector('h1', text: 'Contact') } 
it { should have_selector('title', text: full_title('Contact')) } 
end 
end 

Whe Я бег в $ расслоения Exec RSpec спецификации/запросы/тест static_pages_spec.rb, терминал возвращается эта ошибка:

Failures: 

1) Static pages Home page 
Failure/Error: it { should have_selector('title', text: full_title('')) } 
NoMethodError: 
    undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb4b44504c8> 
# ./spec/requests/static_pages_spec.rb:11:in `block (3 levels) in <top (required)>' 

2) Static pages Help page 
Failure/Error: it { should have_selector('title', text: full_title('Help')) } 
NoMethodError: 
    undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fb4b46a9008> 
# ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>' 

3) Static pages About page 
Failure/Error: it { should have_selector('title', text: full_title('About Us')) } 
NoMethodError: 
    undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007fb4b4430290> 
# ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top (required)>' 

4) Static pages Contact page 
Failure/Error: it { should have_selector('title', text: full_title('Contact')) } 
NoMethodError: 
    undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007fb4b40e57e8> 
# ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>' 

Finished in 0.21306 seconds 
9 examples, 4 failures 

Failed examples: 

rspec ./spec/requests/static_pages_spec.rb:11 # Static pages Home page 
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Help page 
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages About page 
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Contact page 

Любые идеи?

+1

Делали ли вы 'спецификации/поддержка/utilities.rb' с' full_title' методом, как говорит Hartl? – jdoe

+0

Да, сделал это бит –

+0

Все еще не работает –

ответ

1

У вас есть эта строка в файле spec_helper.rb?

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

У вас есть файл spec/support/utilities.rb который выглядит так:

include ApplicationHelper 

def sign_in(user) 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
    # Sign in when not using Capybara as well. 
    cookies[:remember_token] = user.remember_token 
end 

У вас есть приложение/helpers/application_helper.rb файл, который выглядит следующим образом?

module ApplicationHelper 

    # Returns the full title on a per-page basis. 
    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 
end 

Если да, я думаю, что ваша ошибка должна исчезнуть.

+0

Из трех, spec/support/utilities.rb был единственным, что мне нужно было изменить, однако проблема все та же. –

+0

Все еще борется с этим ... –

+0

Хорошо, разобрал проблему с моей ошибкой. spec_helper.rb в не в каталоге поддержки (помещен в каталог спецификаций). Спасибо и извини. –

0

упражнение в главе 5 читает

"eliminate the need for the full_title test helper in Listing 5.26 by writing tests for the original helper method, as shown in Listing 5.37. (You will have to create both the spec/helpers directory and the application_helper_spec.rb file.) Then include it into the test using the code in Listing 5.38."

http://ruby.railstutorial.org/chapters/filling-in-the-layout.html#sec-layout_exercises

ли это, что означает, что я только нужно "включать ApplicationHelper" мой utilites.rb файл, поскольку spec/helpers/application_helper_spec.rb теперь содержит

require 'spec_helper' 

describe ApplicationHelper do 

    describe "full_title" do 
    it "should include the page title" do 
     full_title("foo").should =~ /foo/ 
    end 

Имея эту установку I я все еще получаю сообщение об ошибке при запуске теста RSpec. Они только так я могу получить его, чтобы передать это то, full_title определено в utilites.rb

1

Привет там я также следующий учебник Майкла для Rails 3.2 версии и получить ту же ошибку ... попробуйте это с текущей версией водосвинки

Я изменил : текст к : содержанию и получил это работает, так что это выглядит следующим образом:

it { should have_selector('title', content: full_title('')) } 

Надеется, что это помогает, ура!

0

Текущее форматирование должно выглядеть для ваших тестов, чтобы пройти:

it { should have_title(full_title('Help')) } 

it { should have_title(full_title('About')) } 

it { should have_title(full_title('Contact')) } 
0

Учитывая этот пост два года, я предполагаю, что вы поняли это. Тем не менее, попробуйте добавить

RSpec.configure do |config| 
... 
config.include ApplicationHelper 
... 
end 

в спецификации \ запросов \ spec_helper.rb

Смежные вопросы