Mocha sanity check

23 views
Skip to first unread message

superlou

unread,
Mar 2, 2011, 9:27:56 PM3/2/11
to mocha-developer
Hi folks. I've been starting to pull my hair out and am wondering if
I've missed something straightforward.

I've included "gem 'mocha'" in my Gemfile and verified bundle install
is pulling in 0.9.12.
In a test_helper.rb file, I'm calling "require 'mocha'" as the last
require in the file (after "require 'rails/test_help'" which requires
test/unit).
Then in my unit test file, I do the following:


require 'test_helper'

class ReservationLineTest < ActiveSupport::TestCase

should "save if the resource is not in use during the reservation
period" do
...

resource.expects(:is_free_during).with(reservation.range).returns(true)

assert reservation_line.save
end
end

Since I never call resource.is_free_during, I think I'd expect a
complaint, but the test passes. I also noticed I can't do "object =
moch()" (undefined method mock). I don't receive undefined method
for .expects, .with, .returns, etc.

Has anyone seen this kind of behavior (or is it normal)? I'm pretty
new to mocking but really wanted to get into more serious TDD and
mocha looks like an awesome tool.

Thanks,
Louis

Robert Pankowecki

unread,
Mar 3, 2011, 1:58:40 PM3/3/11
to mocha-d...@googlegroups.com, superlou
Are you sure the test is actually executed ?

Robert

Louis Simons

unread,
Mar 3, 2011, 5:37:31 PM3/3/11
to Robert Pankowecki, mocha-d...@googlegroups.com
Pretty sure.  I've been executing via Rubymine largely, but to double check I did a rake test from a terminal:

Loaded suite /home/lsimons/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.........................

Finished in 0.352315069 seconds.

25 tests, 24 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed


Looks like all of the unit tests pass.  If I put something obviously wrong (ex: assert(false) at the end of the test in question), I get:

Loaded suite /home/lsimons/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
...........F.............

  1) Failure:
test: ReservationLine should save if the resource is not in use during the reservation period. (ReservationLineTest) [test/unit/reservation_line_test.rb:16]:
<false> is not true.

Finished in 0.189861266 seconds.

25 tests, 25 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

Is there a better way to make sure it's executed properly?  I've been doing TDD (hit my first inter-model dependency and wanted some mocks) and it's seemed like test/unit has been behaving.

Thanks,
Louis

superlou

unread,
Mar 3, 2011, 7:29:51 PM3/3/11
to mocha-developer
Solved. Looks like you need mocha after shoulda in your gemfiles.
More details at http://sanjusoftware.wordpress.com/2010/09/22/mocha-should-be-loaded-after-shoulda-with-rails-2-3/.

I changed the relevant section of my gemfile to the following, and
it's complaining nicely about the expectations.

group :test do
gem 'test-unit'
gem 'shoulda'
gem 'mocha'
gem 'factory_girl_rails'
gem 'cucumber'
gem 'cover_me', '>= 1.0.0.rc6'
end

Thanks,
Louis

Jamie Phelps

unread,
Mar 3, 2011, 8:44:29 PM3/3/11
to mocha-developer
Don't you need to make that

gem 'mocha', :require => false

Don't ask me how I know that… :P

Cheers,
Jamie

On Mar 3, 6:29 pm, superlou <lousim...@gmail.com> wrote:
> Solved.  Looks like you need mocha after shoulda in your gemfiles.
> More details athttp://sanjusoftware.wordpress.com/2010/09/22/mocha-should-be-loaded-....

superlou

unread,
Mar 7, 2011, 10:17:52 PM3/7/11
to mocha-developer
Um, actually, I didn't do that part and everything seems to be
working.

I hate to ask, but what's the difference?

James Mead

unread,
May 14, 2011, 6:13:26 AM5/14/11
to mocha-d...@googlegroups.com
The load order for mocha & shoulda is important, because they monkey-patch Test::Unit. If you don't specify the :require => false option in the Gemfile then Bundler will automatically require them and I believe the order is not guaranteed/deterministic [1].

The easiest way to achieve an explicit load order is to use the :require => false option on the relevant gems in the Gemfile (to stop Bundler requiring them) and then add explicit require statements in e.g. your test_helper.rb.

So if you want to make sure your project works deterministically (e.g. on other machines & operating systems), you'd probably be best advised to use the procedure described above i.e. you might just be being lucky that it works at the moment.

I hope that makes sense.

Cheers, James.
----
Reply all
Reply to author
Forward
0 new messages