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)
> 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/