gazay / gon

Your Rails variables in your JS
MIT License
3.05k stars 185 forks source link

Test mode or how to disable `gon` completely for the Test environment / Spec type? #198

Open jibiel opened 8 years ago

jibiel commented 8 years ago

Greetings, gentlemen.

Gon never gave me any pain until last night when I decided to write specs for one of my controller concerns (simplified example):

shared_examples_for :concernable do
    describe '#meffod' do
      let(:controller) { described_class.new }

        it 'assigns a new thingie as @thingie' do
          get :new
          expect(assigns :thingie).to be_a_new Thingie
        end
      end
  end
RSpec.describe ThingiesController do
  describe 'concerns' do
    it_behaves_like :concernable
  end
end
ThingiesController
  concerns
    behaves like concernable
        #meffod
            assigns a new thingie as @thingie (FAILED - 1)

Failures:

  1) ThingiesController concerns behaves like concernable #meffod assigns a new thingie as @thingie
     Failure/Error: get :new
     NoMethodError:
       undefined method `uuid' for nil:NilClass
     Shared Example Group: :concernable called from ./spec/controllers/thingies_controller_spec.rb:44
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@gemset_name/gems/gon-6.0.1/lib/gon/helpers.rb:50:in `gon_request_uuid'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@gemset_name/gems/gon-6.0.1/lib/gon/helpers.rb:33:in `gon'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@gemset_name/gems/gon-6.0.1/lib/gon/spec_helpers.rb:11:in `process'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/actionpack-4.2.4/lib/action_controller/test_case.rb:512:in `get'
     # ./spec/support/shared_examples/controllers/concerns/singular.rb:23:in `block (5 levels) in <top (required)>'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@gemset_name/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
     # /Users/jibiel/.rvm/gems/ruby-2.2.3@global/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
     # -e:1:in `<main>'

Finished in 0.13779 seconds (files took 0.7251 seconds to load)
1 example, 1 failure

So the problem is with the get :new request from within shared_examples_for block. When I write get :new directly in my controller spec, everything's fine.

I also have gon callback disabled for the test environment:

before_action :gonify, unless: -> { Rails.env.test? }

or even have commented it out:

# before_action :gonify

Nevertheless it throws me the above-mentioned NoMethodError: undefined method 'uuid' for nil:NilClass. I've also tried to mock the request object, no go.

Only when I remove gon competely from my Gemfile the requests from the shared_examples_for and the controller specs are starting to show the same behavior.

So my question is do you have any workaround for the cases like this one? Probably the test mode like these thingies have:

# spec/support/geocoder.rb
Geocoder.configure lookup: :test
# spec/support/warden.rb
RSpec.configure do |config|
  config.include Warden::Test::Helpers

  config.before :suite do
    Warden.test_mode!
  end

  config.after :each do
    Warden.test_reset!
  end
end
# spec/support/omniauth.rb
OmniAuth.config.test_mode = true

I guess for now I could explicitly specify the gem for all the environment groups except :test in the Gemfile:

group :development, :staging, :production do
  gem 'gon'
end

but it would be nice to be able to disable gon completely for some types of specs while keeping it intact for the integration specs.

jibiel commented 8 years ago

A similar report.

ghost commented 8 years ago

Thank you for Issue.

So my question is do you have any workaround for the cases like this one? Probably the test mode like these thingies have:

Currently, No, I do not.

but it would be nice to be able to disable gon completely for some types of specs while keeping it intact for the integration specs.

It sounds good iead.

justuseapen commented 7 years ago

Have any workable solutions emerged on this issue?