Search code examples
ruby-on-railsrspecrspec-rails

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


I'm Testing features and need to call a method from ApplicationController, How to call a method gta_data in the test?

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

Solution

  • If you want to call method from application controller from any other controller's spec and your method declared as helper_method then it must be visible from your controller. So you can just use controller.method_name or controller.send(:method_name) if your method is private.

    But if you want to test your method in ApplicationController and to test ApplicationController itself you need to use an anonymous controller rspec approach.

    And. If I understood you right you want to call controller method in your feature specs. Have to tell you that one is not supposed to do it. In your feature specs you just simulate the user's actions in the web-pages and expect the final result which would be visible in your web-pages. It's just like

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