3.1.0.rc4 Mocha expectations don't fail?

94 views
Skip to first unread message

Andre

unread,
Jul 25, 2011, 1:30:58 PM7/25/11
to mocha-developer
I've been trying to get my expectations to fail using rails 3.1.0.rc4,
ruby 1.9.2, mocha-0.9.12

I have the following in my Gemfile

gem 'mocha', :require => false

I have the following in my test_helper.rb

require 'test/unit'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

and the following in my test.rb

config.gem = 'mocha'

I've tried moving the require for test/unit into the test.rb, and a
variety of other combinations. My understanding is that test/unit
just needs to be required before mocha. However, no matter what I do,
expectations do not fail as long as I have 'rails/test_help' included.
As soon as I comment that out, expectations behave normally.

James Mead

unread,
Jul 25, 2011, 5:19:22 PM7/25/11
to mocha-d...@googlegroups.com
Hi Andre,

I'm sorry to hear you've been having problems with Mocha. I've just tried to reproduce the issue with a blank Rails project [1]. There's some more details in the README, but I can't reproduce your problem. I did notice a couple of things that are different :-

* The "require 'test/unit'" line that you have does not appear in the generated test_helper.rb in my project. However, I'm pretty sure the Rails test_help.rb file requires it anyway.
* I think the "config.gem 'mocha'" statement in test.rb is redundant if you are now using Bundler and the Gemfile.

However, I don't think either of these explain the issue you're having. I suspect there may be some other library messing with Test::Unit. Are you using any other testing-related libraries e.g. the test-unit gem, shoulda, etc? If you run your tests using "bundle exec rake MOCHA_OPTIONS=debug", do you see something like this :-

Detected MiniTest version: 1.6.0
Monkey patching MiniTest >= v1.4.2 and <= v1.7.2

Can you try to reproduce the issue in a blank Rails project like mine?

Thanks, James.
----
http://jamesmead.org/

[1] https://github.com/floehopper/rails-mocha

> --
> You received this message because you are subscribed to the Google Groups "mocha-developer" group.
> To post to this group, send email to mocha-d...@googlegroups.com.
> To unsubscribe from this group, send email to mocha-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mocha-developer?hl=en.
>

Andre

unread,
Jul 28, 2011, 5:41:46 PM7/28/11
to mocha-developer
just noticed your link to your rails-mocha project... I think this is
an issue with having turn included. I believe if you add

group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'mocha', :require => false
end

to your gemfile you will see the same behavior as I am.

I get turn included by default when I create a bare bones rails
project with 3.1.rc4.


On Jul 25, 5:19 pm, James Mead <floehop...@gmail.com> wrote:
> Hi Andre,
>
> I'm sorry to hear you've been having problems with Mocha. I've just tried to reproduce the issue with a blank Rails project [1]. There's some more details in the README, but I can't reproduce your problem. I did notice a couple of things that are different :-
>
> * The "require 'test/unit'" line that you have does not appear in the generated test_helper.rb in my project. However, I'm pretty sure the Rails test_help.rb file requires it anyway.
> * I think the "config.gem 'mocha'" statement in test.rb is redundant if you are now using Bundler and the Gemfile.
>
> However, I don't think either of these explain the issue you're having. I suspect there may be some other library messing with Test::Unit. Are you using any other testing-related libraries e.g. the test-unit gem, shoulda, etc? If you run your tests using "bundle exec rake MOCHA_OPTIONS=debug", do you see something like this :-
>
>     Detected MiniTest version: 1.6.0
>     Monkey patching MiniTest >= v1.4.2 and <= v1.7.2
>
> Can you try to reproduce the issue in a blank Rails project like mine?
>
> Thanks, James.
> ----http://jamesmead.org/

Andre

unread,
Jul 28, 2011, 5:32:34 PM7/28/11
to mocha-developer
hi... sorry it took a bit to get back to you.

So I am seeing the same behavior with a fresh application. I can send
you a tar of a basic application where I see the behavior

so the only reason I have the require 'test/unit' is because ''rails/
test_help'' is what requires it, and that is what seems to be causing
a conflict with mocha. If I comment out test_help

#require 'rails/test_help'
require 'test/unit'

and add require 'test/unit', the expectations fail the test when the
method is not called as I would expect them to.

I believe it has something to do with 'turn' being required in
ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/test_help.rb in the
following code:

if defined?(MiniTest)
# Enable turn if it is available
begin
require 'turn'

if MiniTest::Unit.respond_to?(:use_natural_language_case_names=)
MiniTest::Unit.use_natural_language_case_names = true
end
rescue LoadError
end
end

if I comment out the require 'turn' line, things work properly... let
me know what you think.

the following are the changes I made to a fresh rails app.
------------------------
Test Helper
------------------------

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
#require 'test/unit'

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly
in integration tests
# -- they do not yet inherit this setting

# Add more helper methods to be used by all tests here...
end

--------------------
application.rb
--------------------
replaced require 'rails/all' with:

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

-------------------
Gemfile
-------------------
source 'http://rubygems.org'

gem 'rails', '3.1.0.rc4'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

# Asset template engines
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'

gem 'jquery-rails'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'mocha', :require => false
end

----------------------
mocha_test.rb
----------------------

require 'test_helper'

class MochaTest < ActiveSupport::TestCase

test "should fail on expectation" do
TestObject.any_instance.expects(:amethod).once.returns(false)

a = TestObject.new
#puts a.amethod
end

end

class TestObject
def amethod
puts 'here'
end
end

-----------------------
DEBUG OUTPUT
-----------------------

bundle exec rake test MOCHA_OPTIONS=debug
Bundling your gems. This may take a few minutes on first run.
Detected MiniTest version: 1.6.0
Monkey patching MiniTest >= v1.4.2 and <= v1.7.2
Loaded suite /Users/aquina/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/
lib/rake/rake_test_loader
Started

MochaTest:
PASS should fail on expectation (0.00s)

-----------------------
DEBUG OUTPUT WITHOUT 'test/test_help' in test_helper.rb
-----------------------

bundle exec rake test MOCHA_OPTIONS=debug
Bundling your gems. This may take a few minutes on first run.
Detected MiniTest version: 1.6.0
Monkey patching MiniTest >= v1.4.2 and <= v1.7.2
Loaded suite /Users/aquina/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.9.2/
lib/rake/rake_test_loader
Started
F
Finished in 0.001360 seconds.

1) Failure:
test_should_fail_on_expectation(MochaTest) [/Users/aquina/data/
workspaces/hquery/testapp/test/unit/mocha_test.rb:6]:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked:
#<AnyInstance:TestObject>.amethod(any_parameters)




On Jul 25, 5:19 pm, James Mead <floehop...@gmail.com> wrote:
> Hi Andre,
>
> I'm sorry to hear you've been having problems with Mocha. I've just tried to reproduce the issue with a blank Rails project [1]. There's some more details in the README, but I can't reproduce your problem. I did notice a couple of things that are different :-
>
> * The "require 'test/unit'" line that you have does not appear in the generated test_helper.rb in my project. However, I'm pretty sure the Rails test_help.rb file requires it anyway.
> * I think the "config.gem 'mocha'" statement in test.rb is redundant if you are now using Bundler and the Gemfile.
>
> However, I don't think either of these explain the issue you're having. I suspect there may be some other library messing with Test::Unit. Are you using any other testing-related libraries e.g. the test-unit gem, shoulda, etc? If you run your tests using "bundle exec rake MOCHA_OPTIONS=debug", do you see something like this :-
>
>     Detected MiniTest version: 1.6.0
>     Monkey patching MiniTest >= v1.4.2 and <= v1.7.2
>
> Can you try to reproduce the issue in a blank Rails project like mine?
>
> Thanks, James.
> ----http://jamesmead.org/
Reply all
Reply to author
Forward
0 new messages