Stubbing out helpers in rails functional testing / any_instance stubbing on extended things

526 views
Skip to first unread message

Murray Steele

unread,
Oct 9, 2008, 6:13:44 AM10/9/08
to mocha-d...@googlegroups.com
Hi all,

I'm trying to stub out helpers (from app/helpers) in some functional testing but I can't quite get Mocha to cope. 

Having boiled it down, the stream of events is as follows:

class A # In real life, this is ActionView::Base
end

module B # In real life, this is my helper from app/helpers
  def woo
    2
  end
end

A.any_instance.stubs(:woo).returns 3 # I do this in my functional test

a = A.new
a.woo #=> 3
a.extend B  # In real life this happens in ActionController::Base#initialize_template_class
a.woo #=> 2

Anyone have any ideas about how I'd go about stubbing :woo on instances of A given that I won't have an instance of A to play with, so I need to do it with .any_instance.

Cheers,

Murray

ps, I'm using Mocha 0.5.5 would a newer version help?

Ken Collins

unread,
Oct 9, 2008, 11:27:16 AM10/9/08
to mocha-developer

I was just playing around with doing just that, (work is still rails
1.2.6) with latest mocha, and I found found this works.

@controller.class.view_class.any_instance.stubs(:woo).returns(...)

I have not found a way to do it otherwise. I would have thought that
@response.template.stubs(:woo).returns(...) would have worked, but it
did not for me.


- Ken

Murray Steele

unread,
Oct 9, 2008, 12:33:39 PM10/9/08
to mocha-d...@googlegroups.com

2008/10/9 Ken Collins <k...@metaskills.net>



I was just playing around with doing just that, (work is still rails
1.2.6) with latest mocha, and I found found this works.

@controller.class.view_class.any_instance.stubs(:woo).returns(...)

I'll try this and let you know (I'm rails 2.x) on the project I need this for.
 

I have not found a way to do it otherwise. I would have thought that
@response.template.stubs(:woo).returns(...) would have worked, but it
did not for me.

I suspect this is because @response.template when you call .stubs on it will be nil.  It won't get filled in with an actual instance until after you invoke render.  At which point it's too late to do any stubbing.  Well that's assuming that @response.template is the same as @controller.instance_eval { @template } which doesn't get set until during/after render.

Muz

James Mead

unread,
Oct 26, 2008, 3:28:44 PM10/26/08
to mocha-d...@googlegroups.com
2008/10/9 Murray Steele <murray...@gmail.com>

Hi Murray,

Did you ever get this sorted? I'm sorry I haven't been active on the mailing list for a while due to being busy at work, off sick, etc, etc.

It might be worth trying a newer version of Mocha - there've been quite a lot of changes since 0.5.5.

Something else that might be worth a try is stubbing the "new" method itself, i.e. A.stubs(:new).returns(an_instance_of_a).

Anyway, let me know if you are still stuck.

--
James.
http://blog.floehopper.org

Nikos Dimitrakopoulos

unread,
Sep 15, 2011, 7:06:17 AM9/15/11
to mocha-d...@googlegroups.com
I know it's three years later but I just faced this problem. My exact scenario is slightly different : I use a decorator module that get's included in a model instance (through the "extends" method). So for example :

module Decorator
  def foo
  end
end

class Model < ActiveRecord::Base
end

class ModelsController < ApplicationController
  def bar
    @model = Model.find(params[:id])
    @model.extend(Decorator)
    @model.foo
  end
end

Then I would like in the tests to do the following :

test "bar" do
  Model.any_instance.expects(:foo).returns("bar")
  get :bar
end 

Is this possible somehow, or do you have in mind any other way to get this functionality???
Reply all
Reply to author
Forward
0 new messages