bourne 1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ pkg
2
+ *.swp
3
+ .swo
4
+ *~
5
+ tags
6
+ rdoc
7
+ *.gem
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 1.9.2
4
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bourne (1.1.0)
5
+ mocha (= 0.10.4)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ metaclass (0.0.1)
11
+ mocha (0.10.4)
12
+ metaclass (~> 0.0.1)
13
+ rake (0.9.2.2)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bourne!
20
+ rake
@@ -0,0 +1,76 @@
1
+ Bourne
2
+ ======
3
+
4
+ Bourne extends mocha to allow detailed tracking and querying of stub and mock
5
+ invocations. It allows test spies using the have_received rspec matcher and
6
+ assert_received for Test::Unit. Bourne was extracted from jferris-mocha, a fork
7
+ of mocha that adds test spies.
8
+
9
+ Test Spies
10
+ ----------
11
+
12
+ Test spies are a form of test double that preserves the normal four-phase unit
13
+ test order and allows separation of stubbing and verification.
14
+
15
+ Using a test spy is like using a mocked expectation except that there are two steps:
16
+
17
+ 1. Stub out a method for which you want to verify invocations
18
+ 2. Use an assertion or matcher to verify that the stub was invoked correctly
19
+
20
+ Examples
21
+ --------
22
+
23
+ RSpec:
24
+
25
+ mock.should have_received(:to_s)
26
+ Radio.should have_received(:new).with(1041)
27
+ radio.should have_received(:volume).with(11).twice
28
+ radio.should have_received(:off).never
29
+
30
+ You also want to configure RSpec to use mocha for mocking:
31
+
32
+ RSpec.configure do |config|
33
+ config.mock_with :mocha
34
+ end
35
+
36
+ Test::Unit:
37
+
38
+ assert_received(mock, :to_s)
39
+ assert_received(Radio, :new) {|expect| expect.with(1041) }
40
+ assert_received(radio, :volume) {|expect| expect.with(11).twice }
41
+ assert_received(radio, :off) {|expect| expect.never }
42
+
43
+ See Mocha::API for more information.
44
+
45
+ Install
46
+ -------
47
+
48
+ gem install bourne
49
+
50
+ More Information
51
+ ----------------
52
+
53
+ * [RDoc](http://rdoc.info/projects/thoughtbot/bourne)
54
+ * [Issues](http://github.com/thoughtbot/bourne/issues)
55
+ * [Mocha mailing list](http://groups.google.com/group/mocha-developer)
56
+
57
+ Credits
58
+ -------
59
+
60
+ Bourne was written by Joe Ferris. Mocha was written by James Mead. Several of
61
+ the test examples and helpers used in the Bourne test suite were copied
62
+ directly from Mocha.
63
+
64
+ Thanks to thoughtbot for inspiration, ideas, and funding. Thanks to James for
65
+ writing mocha.
66
+
67
+ ![thoughtbot](http://thoughtbot.com/images/tm/logo.png)
68
+
69
+ Thank you to all [the contributors](https://github.com/thoughtbot/bourne/contributors)!
70
+
71
+ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
72
+
73
+ License
74
+ -------
75
+
76
+ Bourne is Copyright © 2010-2011 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
- require 'rake/rdoctask'
2
- require 'rake/gempackagetask'
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
3
  require 'rake/testtask'
4
4
 
5
5
  desc "Run all tests"
6
6
  task 'default' => ['test:units', 'test:acceptance', 'test:performance']
7
7
 
8
8
  namespace 'test' do
9
- unit_tests = FileList['test/unit/**/*_test.rb']
9
+ unit_tests = FileList['test/unit/**/*_test.rb']
10
10
  acceptance_tests = FileList['test/acceptance/*_test.rb']
11
11
 
12
12
  desc "Run unit tests"
@@ -35,27 +35,20 @@ end
35
35
 
36
36
  def benchmark_test_case(klass, iterations)
37
37
  require 'benchmark'
38
- require 'test/unit/ui/console/testrunner'
39
- begin
40
- require 'test/unit/ui/console/outputlevel'
41
- silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT }
42
- rescue LoadError
43
- silent_option = Test::Unit::UI::SILENT
44
- end
45
- time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, silent_option) } }
46
- end
47
-
48
- eval("$specification = #{IO.read('bourne.gemspec')}")
49
- Rake::GemPackageTask.new($specification) do |package|
50
- package.need_zip = true
51
- package.need_tar = true
52
- end
53
38
 
54
- desc 'Generate documentation.'
55
- Rake::RDocTask.new(:rdoc) do |rdoc|
56
- rdoc.rdoc_dir = 'rdoc'
57
- rdoc.title = 'Bourne'
58
- rdoc.options << '--line-numbers' << "--main" << "README"
59
- rdoc.rdoc_files.include('README')
60
- rdoc.rdoc_files.include('lib/**/*.rb')
39
+ if defined?(MiniTest)
40
+ MiniTest::Unit.output = StringIO.new
41
+ Benchmark.realtime { iterations.times { |i| MiniTest::Unit.new.run([klass]) } }
42
+ else
43
+ load 'test/unit/ui/console/testrunner.rb' unless defined?(Test::Unit::UI::Console::TestRunner)
44
+ unless $silent_option
45
+ begin
46
+ load 'test/unit/ui/console/outputlevel.rb' unless defined?(Test::Unit::UI::Console::OutputLevel::SILENT)
47
+ $silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT }
48
+ rescue LoadError
49
+ $silent_option = Test::Unit::UI::SILENT
50
+ end
51
+ end
52
+ Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, $silent_option) } }
53
+ end
61
54
  end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bourne/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'bourne'
7
+ s.version = Bourne::VERSION.dup
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Joe Ferris"]
10
+ s.email = 'jferris@thoughtbot.com'
11
+ s.homepage = 'http://github.com/thoughtbot/bourne'
12
+ s.summary = 'Adds test spies to mocha.'
13
+ s.description = %q{Extends mocha to allow detailed tracking and querying of
14
+ stub and mock invocations. Allows test spies using the have_received rspec
15
+ matcher and assert_received for Test::Unit. Extracted from the
16
+ jferris-mocha fork.}
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency('mocha', '= 0.10.4')
24
+
25
+ s.add_development_dependency('rake')
26
+ end
@@ -16,6 +16,9 @@ module Mocha # :nodoc:
16
16
  assert matcher.matches?(mock), matcher.failure_message
17
17
  end
18
18
 
19
+ class InvalidHaveReceived < StandardError
20
+ end
21
+
19
22
  class HaveReceived #:nodoc:
20
23
  def initialize(expected_method_name)
21
24
  @expected_method_name = expected_method_name
@@ -41,7 +44,12 @@ module Mocha # :nodoc:
41
44
  @expectation.invocation_count = invocation_count
42
45
  @expectation.verified?
43
46
  end
44
-
47
+
48
+ def does_not_match?(mock)
49
+ raise InvalidHaveReceived.new("should_not have_received(:#{@expected_method_name}) is invalid, please use" +
50
+ " should have_received(:#{@expected_method_name}).never")
51
+ end
52
+
45
53
  def failure_message
46
54
  @expectation.mocha_inspect
47
55
  end
@@ -13,9 +13,18 @@ module Mocha # :nodoc:
13
13
  matching_expectation_allowing_invocation.invoke(arguments, &block)
14
14
  else
15
15
  if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
16
+ matching_expectation.invoke(arguments, &block) if matching_expectation
16
17
  message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
17
- message << Mockery.instance.mocha_inspect
18
+ message << @mockery.mocha_inspect
18
19
  raise ExpectationError.new(message, caller)
20
+ else
21
+ target = if self.respond_to? :mocha
22
+ self.mocha
23
+ else
24
+ mocha
25
+ end
26
+ Mockery.instance.invocation(target, symbol, arguments)
27
+ nil
19
28
  end
20
29
  end
21
30
  end
@@ -0,0 +1,3 @@
1
+ module Bourne
2
+ VERSION = '1.1.0'.freeze
3
+ end
@@ -14,99 +14,109 @@ module SpyTestMethods
14
14
 
15
15
  def test_should_accept_wildcard_stub_call_without_arguments
16
16
  instance = new_instance
17
- instance.stubs(:to_s)
18
- instance.to_s
19
- assert_received(instance, :to_s)
20
- assert_matcher_accepts have_received(:to_s), instance
17
+ instance.stubs(:magic)
18
+ instance.magic
19
+ assert_received(instance, :magic)
20
+ assert_matcher_accepts have_received(:magic), instance
21
21
  end
22
22
 
23
23
  def test_should_accept_wildcard_stub_call_with_arguments
24
24
  instance = new_instance
25
- instance.stubs(:to_s)
26
- instance.to_s(:argument)
27
- assert_received(instance, :to_s)
28
- assert_matcher_accepts have_received(:to_s), instance
25
+ instance.stubs(:magic)
26
+ instance.magic(:argument)
27
+ assert_received(instance, :magic)
28
+ assert_matcher_accepts have_received(:magic), instance
29
29
  end
30
30
 
31
31
  def test_should_not_accept_wildcard_stub_without_call
32
32
  instance = new_instance
33
- instance.stubs(:to_s)
34
- assert_fails { assert_received(instance, :to_s) }
35
- assert_fails { assert_matcher_accepts have_received(:to_s), instance }
33
+ instance.stubs(:magic)
34
+ assert_fails { assert_received(instance, :magic) }
35
+ assert_fails { assert_matcher_accepts have_received(:magic), instance }
36
36
  end
37
37
 
38
38
  def test_should_not_accept_call_without_arguments
39
39
  instance = new_instance
40
- instance.stubs(:to_s)
41
- instance.to_s
42
- assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
43
- assert_fails { assert_matcher_accepts have_received(:to_s).with(1), instance }
40
+ instance.stubs(:magic)
41
+ instance.magic
42
+ assert_fails { assert_received(instance, :magic) {|expect| expect.with(1) } }
43
+ assert_fails { assert_matcher_accepts have_received(:magic).with(1), instance }
44
44
  end
45
45
 
46
46
  def test_should_not_accept_call_with_different_arguments
47
47
  instance = new_instance
48
- instance.stubs(:to_s)
49
- instance.to_s(2)
50
- assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
51
- assert_fails { assert_matcher_accepts have_received(:to_s).with(1), instance }
48
+ instance.stubs(:magic)
49
+ instance.magic(2)
50
+ assert_fails { assert_received(instance, :magic) {|expect| expect.with(1) } }
51
+ assert_fails { assert_matcher_accepts have_received(:magic).with(1), instance }
52
52
  end
53
53
 
54
54
  def test_should_accept_call_with_correct_arguments
55
55
  instance = new_instance
56
- instance.stubs(:to_s)
57
- instance.to_s(1)
58
- assert_received(instance, :to_s) {|expect| expect.with(1) }
59
- assert_matcher_accepts have_received(:to_s).with(1), instance
56
+ instance.stubs(:magic)
57
+ instance.magic(1)
58
+ assert_received(instance, :magic) {|expect| expect.with(1) }
59
+ assert_matcher_accepts have_received(:magic).with(1), instance
60
60
  end
61
61
 
62
62
  def test_should_accept_call_with_wildcard_arguments
63
63
  instance = new_instance
64
- instance.stubs(:to_s)
65
- instance.to_s('hello')
66
- assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) }
67
- assert_matcher_accepts have_received(:to_s).with(is_a(String)), instance
64
+ instance.stubs(:magic)
65
+ instance.magic('hello')
66
+ assert_received(instance, :magic) {|expect| expect.with(is_a(String)) }
67
+ assert_matcher_accepts have_received(:magic).with(is_a(String)), instance
68
68
  end
69
69
 
70
70
  def test_should_reject_call_on_different_mock
71
71
  instance = new_instance
72
72
  other = new_instance
73
- instance.stubs(:to_s)
74
- other.stubs(:to_s)
75
- other.to_s('hello')
76
- assert_fails { assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) } }
77
- assert_fails { assert_matcher_accepts have_received(:to_s).with(is_a(String)), instance }
73
+ instance.stubs(:magic)
74
+ other.stubs(:magic)
75
+ other.magic('hello')
76
+ assert_fails { assert_received(instance, :magic) {|expect| expect.with(is_a(String)) } }
77
+ assert_fails { assert_matcher_accepts have_received(:magic).with(is_a(String)), instance }
78
78
  end
79
79
 
80
80
  def test_should_accept_correct_number_of_calls
81
81
  instance = new_instance
82
- instance.stubs(:to_s)
83
- 2.times { instance.to_s }
84
- assert_received(instance, :to_s) {|expect| expect.twice }
85
- assert_matcher_accepts have_received(:to_s).twice, instance
82
+ instance.stubs(:magic)
83
+ 2.times { instance.magic }
84
+ assert_received(instance, :magic) {|expect| expect.twice }
85
+ assert_matcher_accepts have_received(:magic).twice, instance
86
+ end
87
+
88
+ def test_should_not_allow_should_not
89
+ begin
90
+ have_received(:magic).does_not_match?(new_instance)
91
+ rescue Mocha::API::InvalidHaveReceived => exception
92
+ assert_match "should_not have_received(:magic) is invalid, please use should have_received(:magic).never", exception.message, "Test failed, but with the wrong message"
93
+ return
94
+ end
95
+ flunk("Expected to fail")
86
96
  end
87
97
 
88
98
  def test_should_reject_not_enough_calls
89
99
  instance = new_instance
90
- instance.stubs(:to_s)
91
- instance.to_s
100
+ instance.stubs(:magic)
101
+ instance.magic
92
102
  message = /expected exactly twice/
93
- assert_fails(message) { assert_received(instance, :to_s) {|expect| expect.twice } }
94
- assert_fails(message) { assert_matcher_accepts have_received(:to_s).twice, instance }
103
+ assert_fails(message) { assert_received(instance, :magic) {|expect| expect.twice } }
104
+ assert_fails(message) { assert_matcher_accepts have_received(:magic).twice, instance }
95
105
  end
96
106
 
97
107
  def test_should_reject_too_many_calls
98
108
  instance = new_instance
99
- instance.stubs(:to_s)
100
- 2.times { instance.to_s }
109
+ instance.stubs(:magic)
110
+ 2.times { instance.magic }
101
111
  message = /expected exactly once/
102
- assert_fails(message) { assert_received(instance, :to_s) {|expect| expect.once } }
103
- assert_fails(message) { assert_matcher_accepts have_received(:to_s).once, instance }
112
+ assert_fails(message) { assert_received(instance, :magic) {|expect| expect.once } }
113
+ assert_fails(message) { assert_matcher_accepts have_received(:magic).once, instance }
104
114
  end
105
115
 
106
116
  def assert_fails(message=/not yet invoked/)
107
117
  begin
108
118
  yield
109
- rescue Test::Unit::AssertionFailedError => exception
119
+ rescue FailedAssertion => exception
110
120
  assert_match message, exception.message, "Test failed, but with the wrong message"
111
121
  return
112
122
  end
@@ -132,3 +142,19 @@ class PureSpyTest < Test::Unit::TestCase
132
142
  stub
133
143
  end
134
144
  end
145
+
146
+ class StubEverythingSpyTest < Test::Unit::TestCase
147
+ include AcceptanceTest
148
+ def setup
149
+ setup_acceptance_test
150
+ end
151
+
152
+ def teardown
153
+ teardown_acceptance_test
154
+ end
155
+ def test_should_match_invocations_with_no_explicit_stubbing
156
+ instance = stub_everything
157
+ instance.surprise!
158
+ assert_received(instance, :surprise!)
159
+ end
160
+ end
@@ -1,4 +1,4 @@
1
- require 'mocha/metaclass'
1
+ require 'metaclass'
2
2
 
3
3
  module Mocha
4
4
 
@@ -21,4 +21,4 @@ end
21
21
 
22
22
  class Object
23
23
  include Mocha::ObjectMethods
24
- end
24
+ end
@@ -0,0 +1,83 @@
1
+ require 'stringio'
2
+ require 'test/unit/testcase'
3
+ require 'minitest/unit'
4
+
5
+ class MiniTestResult
6
+
7
+ FAILURE_PATTERN = %r{(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n}m
8
+ ERROR_PATTERN = %r{(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n}m
9
+
10
+ def self.parse_failure(raw)
11
+ matches = FAILURE_PATTERN.match(raw)
12
+ return nil unless matches
13
+ Failure.new(matches[2], matches[3], [matches[4]], matches[5])
14
+ end
15
+
16
+ def self.parse_error(raw)
17
+ matches = ERROR_PATTERN.match(raw)
18
+ return nil unless matches
19
+ backtrace = raw.gsub(ERROR_PATTERN, '').split("\n").map(&:strip)
20
+ Error.new(matches[2], matches[3], matches[4], backtrace)
21
+ end
22
+
23
+ class Failure
24
+ attr_reader :method, :test_case, :location, :message
25
+ def initialize(method, test_case, location, message)
26
+ @method, @test_case, @location, @message = method, test_case, location, message
27
+ end
28
+ end
29
+
30
+ class Error
31
+ class Exception
32
+ attr_reader :message, :backtrace
33
+ def initialize(message, location)
34
+ @message, @backtrace = message, location
35
+ end
36
+ end
37
+
38
+ attr_reader :method, :test_case, :exception
39
+ def initialize(method, test_case, message, backtrace)
40
+ @method, @test_case, @exception = method, test_case, Exception.new(message, backtrace)
41
+ end
42
+ end
43
+
44
+ def initialize(runner, test)
45
+ @runner, @test = runner, test
46
+ end
47
+
48
+ def failure_count
49
+ @runner.failures
50
+ end
51
+
52
+ def assertion_count
53
+ @test._assertions
54
+ end
55
+
56
+ def error_count
57
+ @runner.errors
58
+ end
59
+
60
+ def passed?
61
+ @test.passed?
62
+ end
63
+
64
+ def failures
65
+ @runner.report.map { |puked| MiniTestResult.parse_failure(puked) }.compact
66
+ end
67
+
68
+ def errors
69
+ @runner.report.map { |puked| MiniTestResult.parse_error(puked) }.compact
70
+ end
71
+
72
+ def failure_messages
73
+ failures.map(&:message)
74
+ end
75
+
76
+ def failure_message_lines
77
+ failure_messages.map { |message| message.split("\n") }.flatten
78
+ end
79
+
80
+ def error_messages
81
+ errors.map { |e| e.exception.message }
82
+ end
83
+ end
@@ -13,7 +13,11 @@ if ENV['MOCHA_OPTIONS'] == 'use_test_unit_gem'
13
13
  gem 'test-unit'
14
14
  end
15
15
 
16
- require 'rubygems'
17
16
  require 'test/unit'
18
- gem 'mocha', '0.9.8'
17
+ require 'mocha'
19
18
 
19
+ if defined?(MiniTest)
20
+ FailedAssertion = MiniTest::Assertion
21
+ else
22
+ FailedAssertion = Test::Unit::AssertionFailedError
23
+ end
@@ -22,6 +22,9 @@ module TestRunner
22
22
  def failure_messages
23
23
  failures.map { |failure| failure.message }
24
24
  end
25
+ def failure_message_lines
26
+ failure_messages.map { |message| message.split("\n") }.flatten
27
+ end
25
28
  def error_messages
26
29
  errors.map { |error| error.message }
27
30
  end
@@ -44,4 +47,5 @@ module TestRunner
44
47
  flunk "Test passed unexpectedly" if test_result.passed?
45
48
  end
46
49
 
47
- end
50
+ end
51
+
@@ -137,7 +137,7 @@ class AssertReceivedTest < Test::Unit::TestCase
137
137
  begin
138
138
  yield
139
139
  false
140
- rescue Test::Unit::AssertionFailedError
140
+ rescue FailedAssertion
141
141
  true
142
142
  end
143
143
  end
@@ -20,6 +20,9 @@ class ExpectationTest < Test::Unit::TestCase
20
20
  def invocation(mock, method_name, args)
21
21
  @invocations << { :mock => mock, :method_name => method_name, :args => args }
22
22
  end
23
+
24
+ def verify(assertion_counter = nil)
25
+ end
23
26
  end
24
27
 
25
28
  def setup
@@ -317,7 +320,8 @@ class ExpectationTest < Test::Unit::TestCase
317
320
  expectation = new_expectation.times(2)
318
321
  1.times {expectation.invoke([])}
319
322
  assert !expectation.verified?
320
- assert_match(/expected exactly twice, already invoked once/i, expectation.mocha_inspect)
323
+ assert_match(/expected exactly twice/i, expectation.mocha_inspect)
324
+ assert_match(/invoked once/i, expectation.mocha_inspect)
321
325
  end
322
326
 
323
327
  def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
@@ -131,7 +131,7 @@ module HaveReceivedTestMethods
131
131
  begin
132
132
  yield
133
133
  false
134
- rescue Test::Unit::AssertionFailedError
134
+ rescue FailedAssertion
135
135
  true
136
136
  end
137
137
  end
@@ -9,7 +9,7 @@ class MockTest < Test::Unit::TestCase
9
9
  include Mocha
10
10
 
11
11
  def test_should_set_single_expectation
12
- mock = Mock.new
12
+ mock = build_mock
13
13
  mock.expects(:method1).returns(1)
14
14
  assert_nothing_raised(ExpectationError) do
15
15
  assert_equal 1, mock.method1
@@ -17,30 +17,30 @@ class MockTest < Test::Unit::TestCase
17
17
  end
18
18
 
19
19
  def test_should_build_and_store_expectations
20
- mock = Mock.new
20
+ mock = build_mock
21
21
  expectation = mock.expects(:method1)
22
22
  assert_not_nil expectation
23
23
  assert_equal [expectation], mock.expectations.to_a
24
24
  end
25
25
 
26
26
  def test_should_not_stub_everything_by_default
27
- mock = Mock.new
27
+ mock = build_mock
28
28
  assert_equal false, mock.everything_stubbed
29
29
  end
30
-
30
+
31
31
  def test_should_stub_everything
32
- mock = Mock.new
32
+ mock = build_mock
33
33
  mock.stub_everything
34
34
  assert_equal true, mock.everything_stubbed
35
35
  end
36
36
 
37
37
  def test_should_be_able_to_extend_mock_object_with_module
38
- mock = Mock.new
38
+ mock = build_mock
39
39
  assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
40
40
  end
41
41
 
42
42
  def test_should_be_equal
43
- mock = Mock.new
43
+ mock = build_mock
44
44
  assert_equal true, mock.eql?(mock)
45
45
  end
46
46
 
@@ -51,55 +51,55 @@ class MockTest < Test::Unit::TestCase
51
51
  end
52
52
 
53
53
  def test_should_be_able_to_mock_standard_object_methods
54
- mock = Mock.new
54
+ mock = build_mock
55
55
  OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
56
56
  OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
57
57
  assert mock.__verified__?
58
58
  end
59
59
 
60
60
  def test_should_be_able_to_stub_standard_object_methods
61
- mock = Mock.new
61
+ mock = build_mock
62
62
  OBJECT_METHODS.each { |method| mock.__stubs__(method.to_sym).returns(method) }
63
63
  OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
64
64
  end
65
65
 
66
66
  def test_should_create_and_add_expectations
67
- mock = Mock.new
67
+ mock = build_mock
68
68
  expectation1 = mock.expects(:method1)
69
69
  expectation2 = mock.expects(:method2)
70
70
  assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
71
71
  end
72
72
 
73
73
  def test_should_pass_backtrace_into_expectation
74
- mock = Mock.new
74
+ mock = build_mock
75
75
  backtrace = Object.new
76
76
  expectation = mock.expects(:method1, backtrace)
77
77
  assert_equal backtrace, expectation.backtrace
78
78
  end
79
79
 
80
80
  def test_should_pass_backtrace_into_stub
81
- mock = Mock.new
81
+ mock = build_mock
82
82
  backtrace = Object.new
83
83
  stub = mock.stubs(:method1, backtrace)
84
84
  assert_equal backtrace, stub.backtrace
85
85
  end
86
86
 
87
87
  def test_should_create_and_add_stubs
88
- mock = Mock.new
88
+ mock = build_mock
89
89
  stub1 = mock.stubs(:method1)
90
90
  stub2 = mock.stubs(:method2)
91
91
  assert_equal [stub1, stub2].to_set, mock.expectations.to_set
92
92
  end
93
93
 
94
94
  def test_should_invoke_expectation_and_return_result
95
- mock = Mock.new
95
+ mock = build_mock
96
96
  mock.expects(:my_method).returns(:result)
97
97
  result = mock.my_method
98
98
  assert_equal :result, result
99
99
  end
100
100
 
101
101
  def test_should_not_raise_error_if_stubbing_everything
102
- mock = Mock.new
102
+ mock = build_mock
103
103
  mock.stub_everything
104
104
  result = nil
105
105
  assert_nothing_raised(ExpectationError) do
@@ -109,7 +109,7 @@ class MockTest < Test::Unit::TestCase
109
109
  end
110
110
 
111
111
  def test_should_raise_assertion_error_for_unexpected_method_call
112
- mock = Mock.new
112
+ mock = build_mock
113
113
  error = assert_raise(ExpectationError) do
114
114
  mock.unexpected_method_called(:my_method, :argument1, :argument2)
115
115
  end
@@ -120,7 +120,7 @@ class MockTest < Test::Unit::TestCase
120
120
  end
121
121
 
122
122
  def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied
123
- mock = Mock.new
123
+ mock = build_mock
124
124
  mock.expects(:method1)
125
125
  mock.expects(:method2)
126
126
  mock.method1
@@ -128,7 +128,7 @@ class MockTest < Test::Unit::TestCase
128
128
  end
129
129
 
130
130
  def test_should_increment_assertion_counter_for_every_verified_expectation
131
- mock = Mock.new
131
+ mock = build_mock
132
132
 
133
133
  mock.expects(:method1)
134
134
  mock.method1
@@ -144,7 +144,7 @@ class MockTest < Test::Unit::TestCase
144
144
  end
145
145
 
146
146
  def test_should_yield_supplied_parameters_to_block
147
- mock = Mock.new
147
+ mock = build_mock
148
148
  parameters_for_yield = [1, 2, 3]
149
149
  mock.expects(:method1).yields(*parameters_for_yield)
150
150
  yielded_parameters = nil
@@ -153,69 +153,69 @@ class MockTest < Test::Unit::TestCase
153
153
  end
154
154
 
155
155
  def test_should_set_up_multiple_expectations_with_return_values
156
- mock = Mock.new
156
+ mock = build_mock
157
157
  mock.expects(:method1 => :result1, :method2 => :result2)
158
158
  assert_equal :result1, mock.method1
159
159
  assert_equal :result2, mock.method2
160
160
  end
161
161
 
162
162
  def test_should_set_up_multiple_stubs_with_return_values
163
- mock = Mock.new
163
+ mock = build_mock
164
164
  mock.stubs(:method1 => :result1, :method2 => :result2)
165
165
  assert_equal :result1, mock.method1
166
166
  assert_equal :result2, mock.method2
167
167
  end
168
168
 
169
169
  def test_should_keep_returning_specified_value_for_stubs
170
- mock = Mock.new
170
+ mock = build_mock
171
171
  mock.stubs(:method1).returns(1)
172
172
  assert_equal 1, mock.method1
173
173
  assert_equal 1, mock.method1
174
174
  end
175
175
 
176
176
  def test_should_keep_returning_specified_value_for_expects
177
- mock = Mock.new
177
+ mock = build_mock
178
178
  mock.expects(:method1).times(2).returns(1)
179
179
  assert_equal 1, mock.method1
180
180
  assert_equal 1, mock.method1
181
181
  end
182
182
 
183
183
  def test_should_match_most_recent_call_to_expects
184
- mock = Mock.new
184
+ mock = build_mock
185
185
  mock.expects(:method1).returns(0)
186
186
  mock.expects(:method1).returns(1)
187
187
  assert_equal 1, mock.method1
188
188
  end
189
189
 
190
190
  def test_should_match_most_recent_call_to_stubs
191
- mock = Mock.new
191
+ mock = build_mock
192
192
  mock.stubs(:method1).returns(0)
193
193
  mock.stubs(:method1).returns(1)
194
194
  assert_equal 1, mock.method1
195
195
  end
196
196
 
197
197
  def test_should_match_most_recent_call_to_stubs_or_expects
198
- mock = Mock.new
198
+ mock = build_mock
199
199
  mock.stubs(:method1).returns(0)
200
200
  mock.expects(:method1).returns(1)
201
201
  assert_equal 1, mock.method1
202
202
  end
203
203
 
204
204
  def test_should_match_most_recent_call_to_expects_or_stubs
205
- mock = Mock.new
205
+ mock = build_mock
206
206
  mock.expects(:method1).returns(0)
207
207
  mock.stubs(:method1).returns(1)
208
208
  assert_equal 1, mock.method1
209
209
  end
210
210
 
211
211
  def test_should_respond_to_expected_method
212
- mock = Mock.new
212
+ mock = build_mock
213
213
  mock.expects(:method1)
214
214
  assert_equal true, mock.respond_to?(:method1)
215
215
  end
216
216
 
217
217
  def test_should_not_respond_to_unexpected_method
218
- mock = Mock.new
218
+ mock = build_mock
219
219
  assert_equal false, mock.respond_to?(:method1)
220
220
  end
221
221
 
@@ -223,7 +223,7 @@ class MockTest < Test::Unit::TestCase
223
223
  instance = Class.new do
224
224
  define_method(:respond_to?) { |symbol| true }
225
225
  end.new
226
- mock = Mock.new
226
+ mock = build_mock
227
227
  mock.responds_like(instance)
228
228
  assert_equal true, mock.respond_to?(:invoked_method)
229
229
  end
@@ -232,21 +232,18 @@ class MockTest < Test::Unit::TestCase
232
232
  instance = Class.new do
233
233
  define_method(:respond_to?) { |symbol| false }
234
234
  end.new
235
- mock = Mock.new
235
+ mock = build_mock
236
236
  mock.responds_like(instance)
237
237
  assert_equal false, mock.respond_to?(:invoked_method)
238
238
  end
239
239
 
240
240
  def test_should_return_itself_to_allow_method_chaining
241
- mock = Mock.new
241
+ mock = build_mock
242
242
  assert_same mock.responds_like(Object.new), mock
243
243
  end
244
244
 
245
245
  def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder
246
- instance = Class.new do
247
- define_method(:respond_to?) { true }
248
- end.new
249
- mock = Mock.new
246
+ mock = build_mock
250
247
  mock.stubs(:invoked_method)
251
248
  assert_nothing_raised(NoMethodError) { mock.invoked_method }
252
249
  end
@@ -255,7 +252,7 @@ class MockTest < Test::Unit::TestCase
255
252
  instance = Class.new do
256
253
  define_method(:respond_to?) { |symbol| true }
257
254
  end.new
258
- mock = Mock.new
255
+ mock = build_mock
259
256
  mock.responds_like(instance)
260
257
  mock.stubs(:invoked_method)
261
258
  assert_nothing_raised(NoMethodError) { mock.invoked_method }
@@ -266,7 +263,7 @@ class MockTest < Test::Unit::TestCase
266
263
  define_method(:respond_to?) { |symbol| false }
267
264
  define_method(:mocha_inspect) { 'mocha_inspect' }
268
265
  end.new
269
- mock = Mock.new
266
+ mock = build_mock
270
267
  mock.responds_like(instance)
271
268
  mock.stubs(:invoked_method)
272
269
  assert_raises(NoMethodError) { mock.invoked_method }
@@ -277,7 +274,7 @@ class MockTest < Test::Unit::TestCase
277
274
  define_method(:respond_to?) { |symbol| false }
278
275
  define_method(:mocha_inspect) { 'mocha_inspect' }
279
276
  end.new
280
- mock = Mock.new
277
+ mock = build_mock
281
278
  mock.responds_like(instance)
282
279
  mock.stubs(:invoked_method)
283
280
  begin
@@ -288,17 +285,25 @@ class MockTest < Test::Unit::TestCase
288
285
  end
289
286
 
290
287
  def test_should_handle_respond_to_with_private_methods_param_without_error
291
- mock = Mock.new
288
+ mock = build_mock
292
289
  assert_nothing_raised{ mock.respond_to?(:object_id, false) }
293
290
  end
294
291
 
295
292
  def test_should_respond_to_any_method_if_stubbing_everything
296
- mock = Mock.new
293
+ mock = build_mock
297
294
  mock.stub_everything
298
295
  assert mock.respond_to?(:abc)
299
296
  assert mock.respond_to?(:xyz)
300
297
  end
301
298
 
299
+ def test_should_remove_expectation_for_unstubbed_method
300
+ mock = build_mock
301
+ mock.expects(:method1)
302
+ mock.unstub(:method1)
303
+ e = assert_raises(ExpectationError) { mock.method1 }
304
+ assert_match(/unexpected invocation/, e.message)
305
+ end
306
+
302
307
  class FakeExpectation
303
308
  attr_reader :args
304
309
 
@@ -322,8 +327,12 @@ class MockTest < Test::Unit::TestCase
322
327
  expectation = FakeExpectation.new
323
328
  mock.expectations.add expectation
324
329
  mock.send(method, *args)
325
-
326
330
  assert_equal args, expectation.args
327
331
  end
328
-
332
+
333
+ private
334
+
335
+ def build_mock
336
+ Mock.new(nil)
337
+ end
329
338
  end
@@ -73,6 +73,7 @@ class MockeryTest < Test::Unit::TestCase
73
73
  class FakeMethod
74
74
  def stub; end
75
75
  def unstub; end
76
+ def matches?(other); true; end
76
77
  end
77
78
 
78
79
  def test_should_unstub_all_methods_on_teardown
metadata CHANGED
@@ -1,72 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: bourne
3
- version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 0
9
- version: "1.0"
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Joe Ferris
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-06-08 00:00:00 -04:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: mocha
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70334145515340 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - "="
27
- - !ruby/object:Gem::Version
28
- hash: 43
29
- segments:
30
- - 0
31
- - 9
32
- - 8
33
- version: 0.9.8
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.4
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rake
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70334145515340
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70334145514700 !ruby/object:Gem::Requirement
40
28
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
48
33
  type: :development
49
- version_requirements: *id002
50
- description: |-
51
- Extends mocha to allow detailed tracking and querying of
52
- stub and mock invocations. Allows test spies using the have_received rspec
53
- matcher and assert_received for Test::Unit. Extracted from the
54
- jferris-mocha fork.
34
+ prerelease: false
35
+ version_requirements: *70334145514700
36
+ description: ! "Extends mocha to allow detailed tracking and querying of\n stub
37
+ and mock invocations. Allows test spies using the have_received rspec\n matcher
38
+ and assert_received for Test::Unit. Extracted from the\n jferris-mocha fork."
55
39
  email: jferris@thoughtbot.com
56
40
  executables: []
57
-
58
41
  extensions: []
59
-
60
- extra_rdoc_files:
61
- - README
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .travis.yml
46
+ - Gemfile
47
+ - Gemfile.lock
62
48
  - LICENSE
63
- files:
49
+ - README.md
50
+ - Rakefile
51
+ - bourne.gemspec
52
+ - lib/bourne.rb
64
53
  - lib/bourne/api.rb
65
54
  - lib/bourne/expectation.rb
66
55
  - lib/bourne/invocation.rb
67
56
  - lib/bourne/mock.rb
68
57
  - lib/bourne/mockery.rb
69
- - lib/bourne.rb
58
+ - lib/bourne/version.rb
70
59
  - test/acceptance/acceptance_test_helper.rb
71
60
  - test/acceptance/mocha_example_test.rb
72
61
  - test/acceptance/spy_test.rb
@@ -74,6 +63,7 @@ files:
74
63
  - test/execution_point.rb
75
64
  - test/matcher_helpers.rb
76
65
  - test/method_definer.rb
66
+ - test/mini_test_result.rb
77
67
  - test/simple_counter.rb
78
68
  - test/test_helper.rb
79
69
  - test/test_runner.rb
@@ -83,46 +73,45 @@ files:
83
73
  - test/unit/invocation_test.rb
84
74
  - test/unit/mock_test.rb
85
75
  - test/unit/mockery_test.rb
86
- - LICENSE
87
- - Rakefile
88
- - README
89
- has_rdoc: true
90
76
  homepage: http://github.com/thoughtbot/bourne
91
77
  licenses: []
92
-
93
78
  post_install_message:
94
- rdoc_options:
95
- - --title
96
- - Bourne
97
- - --main
98
- - README
99
- - --line-numbers
100
- require_paths:
79
+ rdoc_options: []
80
+ require_paths:
101
81
  - lib
102
- required_ruby_version: !ruby/object:Gem::Requirement
82
+ required_ruby_version: !ruby/object:Gem::Requirement
103
83
  none: false
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- hash: 3
108
- segments:
109
- - 0
110
- version: "0"
111
- required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
89
  none: false
113
- requirements:
114
- - - ">="
115
- - !ruby/object:Gem::Version
116
- hash: 3
117
- segments:
118
- - 0
119
- version: "0"
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
120
94
  requirements: []
121
-
122
95
  rubyforge_project:
123
- rubygems_version: 1.3.7.pre.1
96
+ rubygems_version: 1.8.10
124
97
  signing_key:
125
98
  specification_version: 3
126
99
  summary: Adds test spies to mocha.
127
- test_files: []
128
-
100
+ test_files:
101
+ - test/acceptance/acceptance_test_helper.rb
102
+ - test/acceptance/mocha_example_test.rb
103
+ - test/acceptance/spy_test.rb
104
+ - test/acceptance/stubba_example_test.rb
105
+ - test/execution_point.rb
106
+ - test/matcher_helpers.rb
107
+ - test/method_definer.rb
108
+ - test/mini_test_result.rb
109
+ - test/simple_counter.rb
110
+ - test/test_helper.rb
111
+ - test/test_runner.rb
112
+ - test/unit/assert_received_test.rb
113
+ - test/unit/expectation_test.rb
114
+ - test/unit/have_received_test.rb
115
+ - test/unit/invocation_test.rb
116
+ - test/unit/mock_test.rb
117
+ - test/unit/mockery_test.rb
data/README DELETED
@@ -1,59 +0,0 @@
1
- = Bourne
2
-
3
- Bourne extends mocha to allow detailed tracking and querying of stub and mock
4
- invocations. It allows test spies using the have_received rspec matcher and
5
- assert_received for Test::Unit. Bourne was extracted from jferris-mocha, a fork
6
- of mocha that adds test spies.
7
-
8
- == Test Spies
9
-
10
- Test spies are a form of test double that preserves the normal four-phase unit
11
- test order and allows separation of stubbing and verification.
12
-
13
- Using a test spy is like using a mocked expectation except that there are two steps:
14
-
15
- 1. Stub out a method for which you want to verify invocations
16
- 2. Use an assertion or matcher to verify that the stub was invoked correctly
17
-
18
- == Examples
19
-
20
- # rspec
21
-
22
- mock.should have_received(:to_s)
23
- Radio.should have_received(:new).with(1041)
24
- radio.should have_received(:volume).with(11).twice
25
- radio.should have_received(:off).never
26
-
27
- # Test::Unit
28
-
29
- assert_received(mock, :to_s)
30
- assert_received(Radio, :new) {|expect| expect.with(1041) }
31
- assert_received(radio, :volume) {|expect| expect.with(11).twice }
32
- assert_received(radio, :off) {|expect| expect.never }
33
-
34
- See Mocha::API for more information.
35
-
36
- == Download
37
-
38
- Rubygems:
39
- gem install bourne
40
-
41
- Github: http://github.com/thoughtbot/bourne/tree/master
42
-
43
- == More Information
44
-
45
- * RDoc[http://rdoc.info/projects/thoughtbot/bourne]
46
- * Mocha mailing list[http://groups.google.com/group/mocha-developer?hl=en]
47
- * Issues[http://github.com/thoughtbot/bourne/issues]
48
- * GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS[http://giantrobots.thoughtbot.com]
49
-
50
- == Author
51
-
52
- Bourne was written by Joe Ferris. Mocha was written by James Mead. Several of
53
- the test examples and helpers used in the Bourne test suite were copied
54
- directly from Mocha.
55
-
56
- Thanks to thoughtbot for inspiration, ideas, and funding. Thanks to James for
57
- writing mocha.
58
-
59
- Copyright 2010 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.