How do I stub a path generating method in Rails?

rspec, ruby-on-rails-3, stubbing

Solution

The method `new_post_path` comes from the `Rails.application.routes.url_helpers` module. You need to stub the method on that module

Rails.application.routes.url_helpers.stub(:new_post_path).and_return("this path isn't important")

Problem

I'm writing a view spec, and it renders a view that contains the line (in haml): ``` =link_to new_post_path ``` but the spec fails with: ``` ActionController::RoutingError: No route matches {:action=>"new", :controller=>"post"} ``` I'm trying to stub the `new_post_path` method, as it's not actually important to the view spec, but haven't had any luck. I've tried, within my spec, the following two variations without any luck: ``` stub!(:new_post_path).and_return("this path isn't important") controller.stub!(:new_post_path).and_return("this path isn't important") ```

Original source