Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created May 21, 2011 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floehopper/984564 to your computer and use it in GitHub Desktop.
Save floehopper/984564 to your computer and use it in GitHub Desktop.
Mocha / Autotest / Bundler / Rails integration
class Foo < ActiveRecord::Base
end
class CreateFoos < ActiveRecord::Migration
def self.up
create_table :foos do |t|
t.string :bar
t.timestamps
end
end
def self.down
drop_table :foos
end
end
source 'http://rubygems.org'
gem 'rails', '3.0.7'
gem 'sqlite3'
gem 'autotest'
gem 'autotest-rails'
gem 'mocha', :require => false
require "test_helper"
class FooIntegrationTest < ActionController::IntegrationTest
def test_bar_stubbed
puts "FooIntegrationTest#test_bar_stubbed"
Foo.any_instance.stubs(:bar => "stubbed-bar")
foo = Foo.new(:bar => "bar")
assert_equal "stubbed-bar", foo.bar
end
def test_bar_unstubbed
puts "FooIntegrationTest#test_bar_unstubbed"
foo = Foo.new(:bar => "bar")
assert_equal "bar", foo.bar
end
end
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
fixtures :all
end
require "mocha"
require "test_helper"
class FooTest < Test::Unit::TestCase
def test_bar_stubbed
puts "FooTest#test_bar_stubbed"
Foo.any_instance.stubs(:bar => "stubbed-bar")
foo = Foo.new(:bar => "bar")
assert_equal "stubbed-bar", foo.bar
end
def test_bar_unstubbed
puts "FooTest#test_bar_unstubbed"
foo = Foo.new(:bar => "bar")
assert_equal "bar", foo.bar
end
end
diff --git a/Gemfile b/Gemfile
index 5acd80b..8dc0d57 100644
--- a/Gemfile
+++ b/Gemfile
@@ -4,4 +4,4 @@ gem 'rails', '3.0.7'
gem 'sqlite3'
gem 'autotest'
gem 'autotest-rails'
-gem 'mocha', :require => false
+gem 'mocha'
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 17eb3c2..033a126 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -10,6 +10,4 @@ class ActiveSupport::TestCase
fixtures :all
# Add more helper methods to be used by all tests here...
end
-
-require "mocha"
@floehopper
Copy link
Author

Before applying z.diff patch

Via rake

$ bundle exec rake
(in /private/tmp/mocha-autotest)
Loaded suite /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
FooTest#test_bar_stubbed
.FooTest#test_bar_unstubbed
.
Finished in 0.009834 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
Loaded suite /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
FooIntegrationTest#test_bar_stubbed
.FooIntegrationTest#test_bar_unstubbed
.
Finished in 0.018753 seconds.

2 tests, 2 assertions, 0 failures, 0 errors

Via autotest

$ be autotest -s rails
loading autotest/rails
/Users/jamesmead/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit test/unit/foo_test.rb test/integration/foo_integration_test.rb].each { |f| require f }" | unit_diff -u
/Users/jamesmead/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/pathname.rb:263: warning: `*' interpreted as argument prefix
Loaded suite -e
Started
FooIntegrationTest#test_bar_stubbed
.FooIntegrationTest#test_bar_unstubbed
.FooTest#test_bar_stubbed
.FooTest#test_bar_unstubbed
.
Finished in 0.02502 seconds.

4 tests, 4 assertions, 0 failures, 0 errors

@floehopper
Copy link
Author

After applying z.diff patch

Via rake

$ be rake
(in /private/tmp/mocha-autotest)
Loaded suite /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
FooTest#test_bar_stubbed
.FooTest#test_bar_unstubbed
F
Finished in 0.014879 seconds.

  1. Failure:
    test_bar_unstubbed(FooTest) [/test/unit/foo_test.rb:14]:
    <"bar"> expected but was
    <"stubbed-bar">.

2 tests, 2 assertions, 1 failures, 0 errors
Loaded suite /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
FooIntegrationTest#test_bar_stubbed
.FooIntegrationTest#test_bar_unstubbed
F
Finished in 0.028404 seconds.

  1. Failure:
    test_bar_unstubbed(FooIntegrationTest) [/test/integration/foo_integration_test.rb:14]:
    <"bar"> expected but was
    <"stubbed-bar">.

2 tests, 2 assertions, 1 failures, 0 errors
Errors running test:units, test:integration!

Via autotest

$ be autotest -s rails
loading autotest/rails
/Users/jamesmead/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit test/integration/foo_integration_test.rb test/unit/foo_test.rb].each { |f| require f }" | unit_diff -u
/Users/jamesmead/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/pathname.rb:263: warning: `*' interpreted as argument prefix
Loaded suite -e
Started
FooIntegrationTest#test_bar_stubbed
.FooIntegrationTest#test_bar_unstubbed
.FooTest#test_bar_stubbed
.FooTest#test_bar_unstubbed
.
Finished in 0.021559 seconds.

4 tests, 4 assertions, 0 failures, 0 errors

@floehopper
Copy link
Author

Using Mocha debug option

Via rake

$ MOCHA_OPTIONS=debug be rake
(in /private/tmp/mocha-autotest)
Detected Ruby version: 1.8.7
Detected Test::Unit version: 1.x
Monkey patching Test::Unit for Ruby >= v1.8.6
Loaded suite /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
FooTest#test_bar_stubbed
.FooTest#test_bar_unstubbed
.
Finished in 0.009181 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
Detected Ruby version: 1.8.7
Detected Test::Unit version: 1.x
Monkey patching Test::Unit for Ruby >= v1.8.6
Loaded suite /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
FooIntegrationTest#test_bar_stubbed
.FooIntegrationTest#test_bar_unstubbed
.
Finished in 0.018424 seconds.

2 tests, 2 assertions, 0 failures, 0 errors

Via autotest

$ MOCHA_OPTIONS=debug be autotest -s rails
loading autotest/rails
/Users/jamesmead/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit test/integration/foo_integration_test.rb test/unit/foo_test.rb].each { |f| require f }" | unit_diff -u
/Users/jamesmead/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/pathname.rb:263: warning: `*' interpreted as argument prefix
Detected Ruby version: 1.8.7
Detected Test::Unit version: 1.x
Monkey patching Test::Unit for Ruby >= v1.8.6
Loaded suite -e
Started
FooIntegrationTest#test_bar_stubbed
.FooIntegrationTest#test_bar_unstubbed
.FooTest#test_bar_stubbed
.FooTest#test_bar_unstubbed
.
Finished in 0.019127 seconds.

4 tests, 4 assertions, 0 failures, 0 errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment