How to mock url_for?

Hi,

I am testing a model A, which calls inside url_for method; rspec
complains that url_for is undefined (which I suppose is because url_for
is called outside view’s context)

How can I mock url_for rails method?

Please help,
Thank you.

On Oct 27, 2010, at 12:40 PM, Eugen C. 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
url_for(self)
end
end

$ 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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs