Skip to content
satoru koyanagi edited this page May 23, 2018 · 3 revisions
# spec/support/shared_contexts/gon.rb
shared_context :gon do
  let(:gon) { RequestStore.store[:gon].gon }
  before { Gon.clear }
end
# spec/controllers/thingies_controller_spec.rb
RSpec.describe ThingiesController do
  include_context :gon

  describe 'GET #new' do
    it 'gonifies as expected' do
      get :new, {}, valid_session
      expect(gon['key']).to eq :value
    end
  end
end

If you'd rather not stick with certain controller or action for the gon-related specs (let's say you have a gon-related method in your ApplicationController with before_action :gon_related_method), you could use Anonymous controller approach:

RSpec.describe ApplicationController do
  include_context :gon

  controller do
    def index
      render text: :whatever
    end
  end

  describe '#gon_related_method' do
    it 'gonifies as expected' do
      get :index
      expect(gon['key']).to eq :value
    end
  end
end
Clone this wiki locally