# Rails and Mocha problem

**URL:** <https://www.ruby-forum.com/t/rails-and-mocha-problem/175017>\
**Category:** Rails\
**Created:** [September 6, 2009, 2:00am UTC](https://www.ruby-forum.com/t/rails-and-mocha-problem/175017 "2009-09-06T02:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [September 6, 2009, 2:00am UTC](https://www.ruby-forum.com/t/rails-and-mocha-problem/175017/1 "2009-09-06T02:00:08Z")

</div>

I recently set up an app on a new (old) computer, and I’m having  
problems running my tests. For some reason, mocha is not behaving at  
all. The first thing that happened was that all calls to stub  
(Mocha::API#stub) failed with a

“undefined method `stub’ for #AnyObjectTest:0xb68ae420”.

After some investigation in irb and script/console I tried to “import  
Mocha::API” in my test/test\_helper.rb. That solved my initial problem,  
but brought on

“/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active\_support/dependencies.rb:426:in  
`load\_missing\_constant’: Expected  
/usr/lib/ruby/gems/1.8/gems/mocha-0.9.7/lib/mocha/integration/test\_unit.rb  
to define Mocha::Integration::TestUnit (LoadError)”

So, obviously something’s amiss. I’m on Ubuntu. I installed Mocha  
through RubyGems 1.3.5 (manually installed, not the apt one).

Any ideas on how to fix this?

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [September 6, 2009, 10:13am UTC](https://www.ruby-forum.com/t/rails-and-mocha-problem/175017/2 "2009-09-06T10:13:36Z")

</div>

2009/9/6 Christian J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> but brought on
> 
> “/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active\_support/dependencies.rb:426:in  
> `load\_missing\_constant’: Expected  
> /usr/lib/ruby/gems/1.8/gems/mocha-0.9.7/lib/mocha/integration/test\_unit.rb  
> to define Mocha::Integration::TestUnit (LoadError)”
> 
> So, obviously something’s amiss. I’m on Ubuntu. I installed Mocha  
> through RubyGems 1.3.5 (manually installed, not the apt one).
> 
> Any ideas on how to fix this?

Hi Christian,

I suspect this is a load order problem. After the release of 0.9.6  
[1], Mocha no longer loads Test::Unit itself. Instead Mocha now needs  
you to load Test::Unit _before_ you load Mocha. Exactly how you solve  
this depends on how you are currently loading Test::Unit and Mocha,  
but usually it may simply be a matter of moving a require ‘mocha’  
statement below a require ‘test/unit’ statement.

If you need any more help, please post to the Mocha mailing list [2].

Thanks, James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.

[1] [Mocha Release 0.9.6 - James Mead](http://blog.floehopper.org/articles/2009/06/29/mocha-release-0-9-6)  
[2] [http://groups.google.com/group/mocha-developer](http://groups.google.com/group/mocha-developer)

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [September 6, 2009, 10:38pm UTC](https://www.ruby-forum.com/t/rails-and-mocha-problem/175017/3 "2009-09-06T22:38:46Z")

</div>

Thanks alot! TestUnit was required before Mocha, but somehow I managed  
to require Autotest before that again, and Autotest requires TestUnit as  
well. So, everything works now, thanks 🙂
