简体   繁体   中英

Calling Rails.application.routes.recognize_path within an rspec test does not match any route in Rails 3

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

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

The "/customers" is of course variable.

I'm writing an associated Rspec test, which invokes the code that contains said logic and the test has a complete Rails environment. When I raise the following:

Rails.application.routes.routes.inspect

it contains the proper routes (eg one of the routes it has is "GET /customers").

When I then run the test, the logic results in a:

No route matches "/customers"

Doing the following:

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

results in the same error.

Within a helper test, the following:

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

results in, once again, the same error (No route matches "/customers")

I'm 100% positively sure that Rails.application.routes contain the proper routes.

Does anybody have any idea what the cause of this is?

Thanks!

Finally came across the cause of this stupid self-induced bug: forgot to define a CustomersController that the routes map to.

After diving into Rails' source found out that routing actually constantizes the controller mapped to the routes, so an actual controller to map to is required in your specs. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM