簡體   English   中英

我正在測試功能,需要從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

規格/功能/ 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本身,則需要使用匿名控制器 rspec方法。

和。 如果我理解正確,那么您想在功能說明中調用控制器方法。 必須告訴您,不應該這樣做。 在功能規格中,您僅模擬網頁中用戶的操作,並期望最終結果將在網頁中顯示。 就像

用戶單擊按鈕,它將重定向到帶有“新頁面”標題的頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM