Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

rspec - Using the correct url_for method in a Rails engine spec

I have a request spec in a Rails engine. The view that is rendered calls a route and passes in a hash, i.e. projects_path(:scope => "user"). A route like this will eventually call url_for, but url_for is defined in many places. When running the application or running a request spec in the main app (root level), the call chain ends up at ActionView::RoutingUrlFor#url_for; when running a spec in the engine, however, the call chain ends up at ActionView::Helpers::UrlHelper#url_for.

In Rails 4, the url_for method defined in UrlHelper no longer accepts hash arguments, so I'm left with this error

 Failure/Error: visit projects_opportunity_intakes_path
 ActionView::Template::Error:
   arguments passed to url_for can't be handled. Please require routes or provide your own implementation

What I'm trying to figure out is why the heck it's falling back on ActionView::Helpers::UrlHelper when I run a spec in my engine, but not in any other case. This is quite perplexing. One thing that stands out to me: when I put a breakpoint in my view and evaluate self.class, I normally get something that inherits from ActionView::Base, but when I run a spec from my engine, the class doesn't inherit from anything. I don't know if that's related, but it seems fishy.

Anyone experience this? Know a way around it? Could my modules be getting included in the wrong order? Is there something in spec_helper or in the dummy app initialization that I can do to make sure that the right modules are being used?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same issue when updating to Rails 4, and I discovered that the problem was a model containing the following line:

include Rails.application.routes.url_helpers

Try finding code in your application that includes url_helpers somewhere. Removing that line should help solve the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...