Rails.application.routes.recognize_path call in rspec test does not match route in Rails 3

The Rails 3 application I'm working on contains some logic that calls the following code (which I cannot change):

Rails.application.routes.recognize_path("/customers", :method => :get) 

"/ customers", of course, is a variable.

I am writing an associated Rspec test that calls code containing the specified logic, and the test has a complete Rails environment. When I bring up the following:

 Rails.application.routes.routes.inspect 

it contains the correct routes (for example, one of the routes that it has is "GET / customers").

When I run the test, the logic leads to the following:

 No route matches "/customers" 

Do the following:

 @routes = Rails.application.routes assert_recognizes({:controller => "customers", :action => "index"}, "/customers") 

leads to the same error.

As part of an auxiliary test:

 # this succeeds and returns "/customers" x = helper.customers_path Rails.application.routes.recognize_path(x, :method => :get) 

repeats the same error again (no route matches "/ customers")

I am 100% sure that Rails.application.routes contains the correct routes.

Does anyone know what is the reason for this?

Thanks!

+4
source share
1 answer

Finally I came across the reason for this stupid self-induced error: I forgot to identify the CustomerController to which the routes are bound.

After diving into the Rails source, it turned out that routing actually stabilizes the controller mapped to routes, so your specifications require an actual controller to map. :)

+3
source

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


All Articles