Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stub a path generating method in Rails?

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")
like image 373
jefflunt Avatar asked Dec 20 '22 12:12

jefflunt


1 Answers

If you're not in a view spec, but find yourself needing to stub a path helpers, the following syntax works as of Rails 4.2.5; you'll need to receive_message_chain instead as described here:

https://github.com/rspec/rspec-mocks/issues/1038

To wit:

allow(Rails.application).to receive_message_chain(:routes, :url_helpers, :new_post_path).and_return("this path isn't important")

like image 90
Matt Mitchell Avatar answered Dec 30 '22 07:12

Matt Mitchell