开发者

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

开发者 https://www.devze.com 2023-02-04 03:01 出处:网络
A Rails 3 application I\'m working on contains some logic that invokes the following code (which I cannot change):

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 (e.g. 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?

Thank开发者_开发百科s!


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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消