2012-09-20 10 views
15

ApplicationHelperモジュールにfull_titleメソッドがあるとしたら、RSpecリクエスト仕様でどのようにアクセスできますか?RSpecリクエストでApplication Helperメソッドにアクセスできますか?

私は今、次のコードを持っている:

NoMethodError: undefined method full_title' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000003d43138>

0123:この仕様を実行する上で

app/helpers/application_helper.rb

module ApplicationHelper 

    # Returns the full title on a per-page basis. 
    def full_title(page_title) 
     base_title = "My Site title" 
     logger.debug "page_title: #{page_title}" 
     if page_title.empty? 
     base_title 
     else 
     "#{page_title} - #{base_title}" 
     end 
    end 

spec/requests/user_pages_spec.rb

require 'spec_helper' 

    describe "User Pages" do 
     subject { page } 

     describe "signup page" do 
      before { visit signup_path } 

      it { should have_selector('h2', text: 'Sign up') } 
      it { should have_selector('title', text: full_title('Sign Up')) } 

     end 
    end 

を、私は、このエラーメッセージが表示されます

Michael HartlのRails Tutorialのテストによれば、私は自分のユーザー仕様のアプリケーションヘルパーメソッドにアクセスできるはずです。私はここで何の間違いをしていますか?

+0

を持っていますコードとそれは私のために働く。エラーメッセージにさらに情報を追加できますか?また、あなたが共有できるgithubのレポを持っていますか? –

+0

あなたの 'spec/support'ディレクトリにあなたの' utilities.rb'ファイルを作成しましたか? – veritas1

+0

[your code](https://github.com/movingahead/sample_app)をチェックアウトし、すべての仕様が合格しました。データベースを移行しても、sporkを再起動しませんでしたか? –

答えて

2

本のリスト5.26に従ってspec/support/utilities.rbにヘルパーを作成します。

+4

おそらく、あなたのアプリケーションでそれを使いたいので、それはapplication_helper.rbにあります。それを仕様に移す/それを防止し、それをコピーすることはあまり乾燥しません。ナルティーズはより良いアプローチです。 – Brandon

+1

はい、ヘルパーメソッドの仕様がある場合です。 – veritas1

33

別のオプションは、私は、各宝石の現在の最新バージョンを使用してRuby on Rails Tutorial(Railsの4.0バージョンを)やっている

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

spec_helperに直接それを含めることです。 ApplicationHelperを仕様にどのように含めるべきか、私は似たような問題を経験しました。私は次のコードでの作業それを得た:

スペック/ rails_helper.rb

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

仕様/要求/ user_pages_spec.rb

require 'rails_helper' 

describe "User pages", type: :feature do 
    subject { page } 

    describe "signup page" do 
    before { visit signup_path } 

    it { is_expected.to have_selector('h2', text: 'Sign up') } 
    it { is_expected.to have_selector('title', text: full_title('Sign Up')) } 
    end 
end 

Gemfileは、私はまったく同じ

... 
# ruby 2.2.1 
gem 'rails', '4.2.1' 
... 
group :development, :test do 
    gem 'rspec-rails', '~> 3.2.1' 
    ... 
end 

group :test do 
    gem 'capybara', '~> 2.4.4' 
    ... 
+1

これは受け入れられるはずです回答:DRY – yamori

関連する問題