# Modules and stub

**URL:** <https://www.ruby-forum.com/t/modules-and-stub/168721>\
**Category:** RSpec\
**Created:** [May 25, 2009, 1:15am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721 "2009-05-25T01:15:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [May 25, 2009, 1:15am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721/1 "2009-05-25T01:15:43Z")

</div>

If there is a method defined on a module, how do I stub it?

Imagine I have a module like this:

Module Foo  
Module Bar

```
def self.do_something(path)

```

…  
end

end  
end

Somewhere in the code, there is this call:  
Foo::Bar::do\_something(path)

In my test I want the above call to do nothing, so I tried this:  
before(:each) do  
Foo::Bar.stub!(:do\_something).and\_return  
end

but I confirm that do\_something is getting called by adding a puts in  
it. Do I not have the module/stub syntax correct? How can I isolate  
this further?

I tried looking at the doc ( [http://rspec.rubyforge.org/rspec/1.2.6/](http://rspec.rubyforge.org/rspec/1.2.6/) )  
but I couldn’t really make sense of it.

Thanks,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [May 25, 2009, 5:59am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721/2 "2009-05-25T05:59:38Z")

</div>

On Sun, May 24, 2009 at 7:15 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> Foo::Bar.stub!(:do_something).and_return
> 
> ```
> 
> end
> 
> but I confirm that do\_something is getting called by adding a puts in  
> it. Do I not have the module/stub syntax correct? How can I isolate  
> this further?
> 
> I tried looking at the doc ( [http://rspec.rubyforge.org/rspec/1.2.6/](http://rspec.rubyforge.org/rspec/1.2.6/) )  
> but I couldn’t really make sense of it.

Stub methods on objects, not modules. The method can be one that  
_comes from_ a module, but you need to stub it on the specific object  
that is at play in the example.

module Foo  
def do\_something  
end

class Bar  
include Foo  
end

describe Bar do  
before(:each) do  
@bar = Bar.new  
@bar.stub(:do\_something)  
end  
…  
end

HTH,  
David

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [May 25, 2009, 6:03am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721/3 "2009-05-25T06:03:21Z")

</div>

David C. wrote:

> Stub methods on objects, not modules. The method can be one that  
> _comes from_ a module, but you need to stub it on the specific object  
> that is at play in the example.

That makes sense, except the code does this:  
Foo::Bar::do\_something(path)

I’m not an expert with modules, but that looks like it is calling the  
method directly on the module without an object. Is there any way for  
me to stub that? or is that not a good thing to be doing?

Thanks,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [May 25, 2009, 7:23am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721/4 "2009-05-25T07:23:49Z")

</div>

On Mon, May 25, 2009 at 12:03 AM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> David C. wrote:
> 
> > Stub methods on objects, not modules. The method can be one that  
> > _comes from_ a module, but you need to stub it on the specific object  
> > that is at play in the example.
> 
> That makes sense, except the code does this:  
> Foo::Bar::do\_something(path)
> 
> I’m not an expert with modules, but that looks like it is calling the  
> method directly on the module without an object.

You’re correct.

> Is there any way for me to stub that?

You can stub that like this:

Foo::Bar.stub(:do\_something)

> or is that not a good thing to be doing?

This really depends on a lot of different factors. Whether Bar is a  
module or a class, do\_something is essentially a global, and globals  
are _generally_ problematic for testing because they increase the risk  
of leaking state from example to example. That said, the four mock  
frameworks supported directly by rspec (rspec, mocha, flexmock and rr)  
all roll back stubs after each example and they all seem to work just  
fine. Therefore that risk is mitigated. But that doesn’t mean it’s  
100% risk-free. Ruby is very flexible and powerful, and just as  
mock/stub frameworks can momentarily change an object’s behaviour, so  
can any library code or application code.

If you stub, for example, do\_something on this module, but it turns  
out that do\_something gets added to the module through some dynamic  
means _after the stub declaration_, the stub declaration will be  
overwritten and you’ll get surprising results. This is not just true  
of globals - it’s true of any methods that appear through the magic of  
metaprogramming.

HTH,  
David

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [May 27, 2009, 11:00am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721/5 "2009-05-27T11:00:03Z")

</div>

On Mon, May 25, 2009 at 1:51 AM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > of globals - it’s true of any methods that appear through the magic of  
> > metaprogramming.
> 
> Ah, the joys of dynamic languages. After further looking at the code, I  
> see that the module ‘require’ is intentionally inside an if, so that it  
> will only get loaded as needed. If I explicitly require file containing  
> the module in the spec.rb file, then it works. I suppose the require in  
> the code I’m testing will only have an effect when it’s not present in  
> the calling code. Still puzzled why I could call the module before  
> without getting a syntax error.

By “before” do you mean in an earlier version of your app or of rspec?  
Or do you mean within the app somewhere?

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [May 25, 2009, 7:51am UTC](https://www.ruby-forum.com/t/modules-and-stub/168721/6 "2009-05-25T07:51:02Z")

</div>

David C. wrote:

> Foo::Bar.stub(:do\_something)

hmm. that’s pretty close to where I started.

> If you stub, for example, do\_something on this module, but it turns  
> out that do\_something gets added to the module through some dynamic  
> means _after the stub declaration_, the stub declaration will be  
> overwritten and you’ll get surprising results. This is not just true  
> of globals - it’s true of any methods that appear through the magic of  
> metaprogramming.

Ah, the joys of dynamic languages. After further looking at the code, I  
see that the module ‘require’ is intentionally inside an if, so that it  
will only get loaded as needed. If I explicitly require file containing  
the module in the spec.rb file, then it works. I suppose the require in  
the code I’m testing will only have an effect when it’s not present in  
the calling code. Still puzzled why I could call the module before  
without getting a syntax error.

Thanks so much for your help.

Sarah
