# Problem with FlexMock::TestCase#flexstub in 0.4.0

**URL:** <https://www.ruby-forum.com/t/problem-with-flexmock-testcase-flexstub-in-0-4-0/67682>\
**Category:** Ruby\
**Created:** [September 6, 2006, 12:58am UTC](https://www.ruby-forum.com/t/problem-with-flexmock-testcase-flexstub-in-0-4-0/67682 "2006-09-06T00:58:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [September 6, 2006, 12:58am UTC](https://www.ruby-forum.com/t/problem-with-flexmock-testcase-flexstub-in-0-4-0/67682/1 "2006-09-06T00:58:48Z")

</div>

Jim,

I’m really glad to see the stubbing capabilities added to FlexMock.  
However, I’m having a problem with the flexstub method.

I have several domain classes (ActiveRecord) in my application, and I  
am working on functionality that will import data into the application  
from a de-normalized tab-separated file we get from our customer on an  
ongoing basis. Each line in the file represents a Product, so the code  
calls Product.import( data\_hash\_from\_line ) for each line in the file.  
Product.import then calls the import method on several related  
classes, giving each one the responsibility for knowing how to handle  
its portion of the data.

For the unit tests, I’m using FlexMock to stub out the calls to import  
in the other classes. If I run a single test case using `ruby test/unit/my_test_file.rb`, everything works as expected. However,  
when I try to run the whole test suite using `rake` (this is a Rails  
app, BTW), I start to see a lot of errors like this:

1. Error:  
test\_should\_return\_status\_if\_it\_exists(StatusTest::Import):  
NoMethodError: undefined method `import' for #<FlexMock:0x236a0e0> /usr/local/lib/ruby/gems/1.8/gems/flexmock-0.4.0/lib/flexmock.rb:113:in `method\_missing’  
/usr/local/lib/ruby/gems/1.8/gems/flexmock-0.4.0/lib/flexmock.rb:228:in  
`mock_wrap' /usr/local/lib/ruby/gems/1.8/gems/flexmock-0.4.0/lib/flexmock.rb:108:in `method\_missing’  
(eval):3:in `import' (eval):5:in `import’  
/Users/jwilger/projects/application/test/test\_helper.rb:70:in  
`import' ./test/unit/status_test.rb:55:in `test\_should\_return\_status\_if\_it\_exists’

It looks like the FlexMock::TestCase#flexmock\_teardown method is  
removing the stub for Status.import, but it isn’t restoring the  
original method.

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [September 6, 2006, 1:46am UTC](https://www.ruby-forum.com/t/problem-with-flexmock-testcase-flexstub-in-0-4-0/67682/2 "2006-09-06T01:46:50Z")

</div>

On 9/5/06, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For the unit tests, I’m using FlexMock to stub out the calls to import  
> in the other classes. If I run a single test case using `ruby test/unit/my_test_file.rb`, everything works as expected. However,  
> when I try to run the whole test suite using `rake` (this is a Rails  
> app, BTW), I start to see a lot of errors like this:

> 1. Error:  
> test\_should\_return\_status\_if\_it\_exists(StatusTest::Import):  
> NoMethodError: undefined method `import’ for #FlexMock:0x236a0e0

> It looks like the FlexMock::TestCase#flexmock\_teardown method is  
> removing the stub for Status.import, but it isn’t restoring the  
> original method.

Bad form replying to myself, I know – but I think I figured out  
what’s going on…

In my tests, I’ve been been doing this:

class MyTest \< Test::Unit::TestCase  
include FlexMock::TestCase

```
def setup
  # set up a default stub
  flexstub( SomeClass ).should_receive( :foo )
end

def test_method_that_calls_foo_but_not_interested_in_foo_a
  SomeOtherClass.do_something_that_calls_foo
  # other assertions, etc
end

def test_method_that_calls_foo_but_not_interested_in_foo_b
  SomeOtherClass.do_something_that_calls_foo
  # other assertions, etc
end

def test_method_that_calls_foo_and_cares
  flexstub( SomeClass ).should_receive( :foo ).once.with( :bar )
  SomeOtherClass.do_something_that_calls_foo
end

```

end

So, I want to be able to set up a default stub for SomeClass.foo, so  
that I can test SomeOtherClass.do\_something\_that\_calls\_foo without  
using the actual implimentation of SomeClass.foo, but I don’t want to  
have to write that default stub out in each test method.

The problem is that when test\_method\_that\_calls\_foo\_and\_cares is run,  
it thinks the default stub is the _original_ method, and when the test  
is finished running, it’s the default stub method that ends up being  
restored.

In order to make this work, we just need to make sure that the  
original method isn’t overwritten if we make more than one call to  
#should\_receive for a particular stub. It’s a pretty simple change on  
line 1008 of lib/flexmock.rb. Just change:

@method\_definitions[method\_name] = @obj.method(method\_name)

to:

@method\_definitions[method\_name] ||= @obj.method(method\_name)

All existing unit tests for the FlexMock library still pass with that  
change in place (though I’ve not yet written any tests to cover this  
case), _and_ it solves the problem I was having with FlexMock in my  
own application.

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer
