我正在测试功能并需要从 ApplicationController 调用方法,我该怎么做?

I'm Testing features and need to call a method from ApplicationController, how can I do that?

我正在测试功能,需要从 ApplicationController 调用方法,如何在测试中调用方法 gta_data

def gtm_data(data_hash = nil)
  @gtm_data ||= {}
  if @dynamic_page
    gaPageUid = 'category-' + @dynamic_page.uid
  else
    gaPageUid = 'other'
  end

  @gtm_data = {
    gaLocale: @locale, gaLanguage: @lang,
    gaRegion: current_site.region, gaPageUid: gaPageUid
  }
  @gtm_data
end

Spec/features/application_page.rb

describe 'Application Page' do
  before :each do
    @application_page = FactoryGirl.create(:application_page, meta_title: 'catched!')
  end
  describe 'gaming layout' do
    before :each do
      @product = Presenters::Product.new(FactoryGirl.create(:product))
      @page_url = "http://de-de.test.de/application_pages/#{@application_page.id}"
    end
    it 'ignores the serie version if have not sufficient data' do
      visit @page_url
      expect(page).to have_content @product.name
    end
  end
end

如果您想从任何其他控制器的规范中调用应用程序控制器的方法,并且您的方法声明为 helper_method,那么它必须在您的控制器中可见。因此,如果您的方法是私有的,您可以只使用 controller.method_namecontroller.send(:method_name)

但是如果您想在 ApplicationController 中测试您的方法并测试 ApplicationController 本身,您需要使用 anonymous controller rspec 方法。

还有。如果我理解你是对的,你想在你的特性规范中调用控制器方法。必须告诉你,一个人不应该这样做。在您的功能规范中,您只需模拟用户在 web-pages 中的操作,并期望最终结果在您的 web-pages 中可见。就像

users clicks on button and it redirects to the page with the "New page" header