# Mocking, a common practice

**URL:** <https://www.ruby-forum.com/t/mocking-a-common-practice/187448>\
**Category:** Rails\
**Created:** [April 23, 2010, 9:35am UTC](https://www.ruby-forum.com/t/mocking-a-common-practice/187448 "2010-04-23T09:35:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mariek](https://www.ruby-forum.com/u/mariek)\
**Post date:** [April 23, 2010, 9:35am UTC](https://www.ruby-forum.com/t/mocking-a-common-practice/187448/1 "2010-04-23T09:35:23Z")

</div>

I would like to ask for Your experience how to handle mocking class  
method only for one specific test.

Scenario is:  
I have unit test suite and in one of the test I have mocked a class  
method, and I want this class method to be only mocked in this  
specific test.  
But this mock applies to all test files while I run rake test:units.

I thought about doing like this with setup and teardown:

class SomeTest \< …

def setup  
#here ovveride class method body in runtime  
end

def teardown  
#and here bring back class method body in runtime  
end

end

But I’m not sure if this is the way it should be done ?

And the reason why I need to do it this way is because in some  
particular tests I need to mock the class method and in other i don’t.  
In my case it is about calls to sandbox. In some tests I check if they  
are made correctly and in other I test only logic flow and I don’t  
want to make real calls to sandbox.

Thanks for any ideas.

---

<div class="post-metadata">

**Author:** ![mariek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mariek](https://www.ruby-forum.com/u/mariek)\
**Post date:** [April 23, 2010, 11:10am UTC](https://www.ruby-forum.com/t/mocking-a-common-practice/187448/2 "2010-04-23T11:10:48Z")

</div>

On Apr 23, 8:33 am, mariek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And the reason why I need to do it this way is because in some  
> particular tests I need to mock the class method and in other i don’t.  
> In my case it is about calls to sandbox. In some tests I check if they  
> are made correctly and in other I test only logic flow and I don’t  
> want to make real calls to sandbox.
> 
> Thanks for any ideas.

use something like mocha, rr or the mocking in rspec ?

Fred

---

<div class="post-metadata">

**Author:** ![mariek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mariek](https://www.ruby-forum.com/u/mariek)\
**Post date:** [April 23, 2010, 11:47am UTC](https://www.ruby-forum.com/t/mocking-a-common-practice/187448/3 "2010-04-23T11:47:41Z")

</div>

Check out flexmock as well

[http://flexmock.rubyforge.org/](http://flexmock.rubyforge.org/)

On Fri, Apr 23, 2010 at 11:09 AM, Frederick C.
