Gem tests cannot find route using url_for

I think the dummy application in which my gem tests are intended to run is not configured properly, because when I call url_for on the Gadget instance (the stub model from the dummy application) inside the gem helper, I get

 undefined method `gadgets_path' for #<#<Class:0x007fe274bc1228>:0x007fe273d45eb0> 

Background: I branched out a gem and made significant changes. ( Here's the plug .) Now I'm trying to run rspec tests so that I can check for my updates in the future.

Tests are configured as a Rails engine with a dummy application in the spec directory. This application has one model ( Gadget ) with the corresponding controller and resource declared in the file spec/dummy/environment/routes.rb :

 Dummy::Application.routes.draw do resources :gadgets end 

The spec/spec_helper.rb as follows:

 ENV["RAILS_ENV"] ||= "test" require File.expand_path("../dummy/config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' RSpec.configure do |config| config.mock_framework = :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false config.order = "random" config.include Rails.application.routes.url_helpers end 

(In fact, you can see the full test setup in the github repo project . I actually opened the question for this week or so, but only now I'm going to solve it.)

One test that is not pending creates an instance of Gadget and then calls the helper with it as an argument. When the helper tries url_for(@gadget) , it causes the above error.

What is wrong here?

ETA Dec 04: Updated with current spec_helper.rb .

+4
source share
2 answers

Update

put this in your spec_helper.rb - at least it works for me (I cloned your repo)

 ActionView::TestCase::TestController.instance_eval do helper Rails.application.routes.url_helpers#, (append other helpers you need) end ActionView::TestCase::TestController.class_eval do def _routes Rails.application.routes end end 

The real problem was that TestController was subclassed from ActionController::Base before the ActionController::Base was extended using helper route methods.
Therefore, you need to enter it back into the TestController . In addition, AbstractController :: UrlFor requires the execution of _routes .


To use routing helpers, you must insert

 Rspec.configure do |config| config.include Rails.application.routes.url_helpers ... end 

in your spec_helper.rb, which makes all available something_path methods available. Another way around the real problem was to drown out the helper as follows:

 helper.stub!(:url_for).and_return("/path") 
+9
source

Although this is quite a bit of source code, it seems to me that you are calling editable_field , which in turn is calling url_for . However, url_for will only work in the context of the controller, whereas you just call it in the middle of the specification.

Thus, perhaps this would be the removal of this method or conducting an integration test. This would be a good workaround.

+1
source

Source: https://habr.com/ru/post/1447258/


All Articles