0

私は現在Hartlを通して働いています。第 5 章では、リスト 5.27 (下記) から Hartl のコードを私の 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_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

$ bundle exec rspec spec/requests/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

何か案は?

4

5 に答える 5

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

このようなapp/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

もしそうなら、私はあなたのエラーがなくなるはずだと思います。

于 2012-06-01T19:43:14.470 に答える
1

こんにちは、私も Rails 3.2 バージョンの Michael のチュートリアルに従っていますが、同じエラーが発生します...現在のバージョンのカピバラでこれを試してください

:text:contentに変更して動作させると、次のようになります。

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

それが役に立てば幸いです、乾杯!

于 2013-09-25T06:08:27.220 に答える
0

第 5 章の演習は次のとおりです。

"リスト 5.37 に示すように、元のヘルパー メソッドのテストを記述して、リスト 5.26の full_title テスト ヘルパーを不要にしapplication_helper_spec.rbます (spec/helpers ディレクトリとファイルの両方を作成する必要があります)。リスト 5.38 のコードを使用します。」

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

utilites.rbそれは、ファイルspec/helpers/application_helper_spec.rbに「ApplicationHelperを含める」だけでよいということですか?

require 'spec_helper'

describe ApplicationHelper do

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

このセットアップを行っても、RSpec テストを実行するとエラーが発生します。私がそれを通過させることができる唯一の方法は、full_title定義することですutilites.rb

于 2012-09-27T19:57:20.043 に答える
0

テストに合格するには、現在のフォーマットは次のようになります。

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

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

it { should have_title(full_title('Contact')) }
于 2014-03-06T00:58:40.433 に答える