19

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

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

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

この仕様を実行すると、次のエラー メッセージが表示されます。

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

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

4

3 に答える 3

40

別のオプションは、それを直接 spec_helper に含めることです

RSpec.configure do |config|
  ...
  config.include ApplicationHelper
end
于 2013-06-12T19:31:55.947 に答える
10

Ruby on Rails チュートリアル(Rails 4.0 バージョン) を、各 gem の現在の最新バージョンを使用して行っています。ApplicationHelper を仕様に含める方法について疑問に思っている同様の問題が発生しました。次のコードで動作するようになりました:

spec/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'
  ...
于 2015-05-19T22:59:11.183 に答える
1

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

于 2012-09-20T12:33:37.910 に答える