I have a function that sends a variable to js with the help of gon.
def calc_foo
# calculate foo
gon.foo = foo
end
I want to test this function i.e make sure that the method return the correct value, using rspec.
it "should return bar" do
foo = @foo_controller.calc_foo
expect(foo).to eq(bar)
end
But, I get the following error message when the test case reaches the line where the variable is sent to gon.
Failure/Error: foo = @foo_controller.calc_foo
NoMethodError:
undefined method `uuid' for nil:NilClass
I have checked the value for foo, and it is not Nil, so gon must be Nil. I believe the error is that I don't incude gon correctly. This is the rspec-part of my Gemfile
#rspec-rails includes RSpec itself in a wrapper to make it play nicely with Rails.
#Factory_girl replaces Rails’ default fixtures for feeding test data
#to the test suite with much more preferable factories.
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
gem 'gon'
end
So how can I get rspec to play nicely with gon? (I have also tried to include gon in my spec-file with no success)
In the controller specs (where
gon
belongs) you'll need to make an actual request to bypass your problem:If you'd rather not stick with certain
controller
oraction
for thegon
-related specs (let's say you have agon
-related method in yourApplicationController
), you could use Anonymous controller approach:I have lots of
controller
andrequest
/integration
specs and can confirm thatgon
behaves fine there as long as you make actual requests.However, I still have a problem similar to yours although the case is different (making requests from the
shared_examples
included in thecontroller
specs). I've opened the relevant issue, feel free to join the conversation (for anyone interested).