[rspec-users] how to mock url_for ?

David Chelimsky dchelimsky at gmail.com
Thu Oct 28 07:06:43 EDT 2010

On Oct 27, 2010, at 12:40 PM, Eugen Ciur wrote:

> Hi,
> I am testing a model A, which calls inside url_for method; rspec
> complains that url_for is undefined

Sometimes error messages tell you exactly what you need to know. In this case, url_is not defined in ActiveRecord models. This is true whether you're in a spec or in the console:

class Gadget < ActiveRecord::Base
  def gimme_your_url

$ script/rails c
Loading development environment (Rails 3.0.2.pre)
ruby-1.8.7-p302 > Gadget.new.gimme_your_url
NoMethodError: undefined method `url_for' for #<Gadget id: nil, created_at: nil, updated_at: nil>

> (which I suppose is because url_for
> is called outside view's context)
> How can I mock url_for rails method?

Stubbing this doesn't make sense if it's not normally available in the model. Why do you want to call url_for from a model instead of a view template or a helper?

More information about the rspec-users mailing list