mocha 0.9.10 → 0.9.11
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.
- data/.gemtest +0 -0
- data/README.rdoc +3 -3
- data/RELEASE.rdoc +9 -1
- data/Rakefile +7 -4
- data/lib/mocha/backtrace_filter.rb +2 -2
- data/lib/mocha/expectation.rb +3 -3
- data/lib/mocha/integration/mini_test.rb +11 -5
- data/lib/mocha/integration/mini_test/{version_142_and_above.rb → version_142_to_172.rb} +2 -2
- data/lib/mocha/integration/mini_test/version_200.rb +57 -0
- data/lib/mocha/integration/mini_test/version_201_to_202.rb +57 -0
- data/lib/mocha/mock.rb +1 -0
- data/lib/mocha/parameter_matchers.rb +1 -0
- data/lib/mocha/parameter_matchers/query_string.rb +47 -0
- data/test/acceptance/exception_rescue_test.rb +3 -6
- data/test/acceptance/expected_invocation_count_test.rb +45 -9
- data/test/acceptance/github_issue_20_test.rb +45 -0
- data/test/acceptance/multiple_expectations_failure_message_test.rb +68 -0
- data/test/acceptance/parameter_matcher_test.rb +54 -0
- data/test/test_runner.rb +12 -5
- data/test/unit/expectation_test.rb +1 -1
- metadata +11 -5
data/.gemtest
ADDED
File without changes
|
data/README.rdoc
CHANGED
@@ -6,7 +6,7 @@ It can be used with many testing frameworks e.g. {Test::Unit}[http://www.ruby-do
|
|
6
6
|
|
7
7
|
Mocha provides a unified, simple and readable syntax for both traditional mocking and partial mocking.
|
8
8
|
|
9
|
-
Mocha was harvested from projects at {Reevoo}[http://www.reevoo.com] by me ({James}[http://
|
9
|
+
Mocha was harvested from projects at {Reevoo}[http://www.reevoo.com/] by me ({James}[http://jamesmead.org/]) and my (then) colleagues {Ben}[http://www.techbelly.com/], {Chris}[http://chrisroos.co.uk/] and {Paul}[http://po-ru.com/].
|
10
10
|
|
11
11
|
== Download and Installation
|
12
12
|
|
@@ -14,7 +14,7 @@ Install the gem with the following command...
|
|
14
14
|
|
15
15
|
$ gem install mocha
|
16
16
|
|
17
|
-
Or install the {Rails}[http://www.rubyonrails.org] plugin...
|
17
|
+
Or install the {Rails}[http://www.rubyonrails.org/] plugin...
|
18
18
|
|
19
19
|
$ script/plugin install git://github.com/floehopper/mocha.git
|
20
20
|
|
@@ -29,7 +29,7 @@ Or download Mocha...
|
|
29
29
|
* Quick Start - {Usage Examples}[http://mocha.rubyforge.org/examples/misc.html]
|
30
30
|
* Traditional mocking - {Star Trek Example}[http://mocha.rubyforge.org/examples/mocha.html]
|
31
31
|
* Setting expectations on real classes - {Order Example}[http://mocha.rubyforge.org/examples/stubba.html]
|
32
|
-
* More examples on {
|
32
|
+
* More examples on {James Mead's Blog}[http://jamesmead.org/blog/]
|
33
33
|
* {Mailing List Archives}[http://groups.google.com/group/mocha-developer]
|
34
34
|
|
35
35
|
== License
|
data/RELEASE.rdoc
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
= 0.9.
|
1
|
+
= 0.9.11 ()
|
2
|
+
* Added explicit support for minitest v1.5.0 to v2.0.2.
|
3
|
+
* Make testable by rubygems-test.
|
4
|
+
* Update links to my blog and make other links consistent.
|
5
|
+
* Added a URI parameter matcher that ignores the order of query parameters so that tests can be independent of undefined hash ordering (patch by Paul Battley).
|
6
|
+
* Include unexpected invocation in failure message and change the language slightly to make the failure message less confusing. See http://floehopper.lighthouseapp.com/projects/22289/tickets/52.
|
7
|
+
* No need to create regular expression every time the BacktraceFilter#filtered method is called. See http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/66.
|
8
|
+
|
9
|
+
= 0.9.10 (31182ea8b38b79aa50702aa839f6a29ebcf7d684)
|
2
10
|
* Added Mocha::ObjectMethods#unstub method - https://github.com/floehopper/mocha/issues#issue/6
|
3
11
|
* Inherit Mocha::ExpectationError from Exception instead of StandardError to reduce the chances of a test passing by accident - thanks to James Sanders (jsanders) - https://github.com/floehopper/mocha/issues#issue/15
|
4
12
|
* Fixed bug - GitHub README page to link correctly to code examples - https://github.com/floehopper/mocha/issues/closed#issue/11
|
data/Rakefile
CHANGED
@@ -3,14 +3,17 @@ require 'rake/gempackagetask'
|
|
3
3
|
require 'rake/testtask'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
VERSION = "0.9.
|
6
|
+
VERSION = "0.9.11"
|
7
7
|
end
|
8
8
|
|
9
9
|
desc "Run all tests"
|
10
|
-
task 'default' => ['test
|
10
|
+
task 'default' => ['test', 'test:performance']
|
11
|
+
|
12
|
+
desc "Run unit & acceptance tests"
|
13
|
+
task 'test' => ['test:units', 'test:acceptance']
|
11
14
|
|
12
15
|
namespace 'test' do
|
13
|
-
|
16
|
+
|
14
17
|
unit_tests = FileList['test/unit/**/*_test.rb']
|
15
18
|
acceptance_tests = FileList['test/acceptance/*_test.rb']
|
16
19
|
|
@@ -162,7 +165,7 @@ def build_specification(version = Mocha::VERSION)
|
|
162
165
|
s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README.rdoc' << '--line-numbers'
|
163
166
|
|
164
167
|
s.add_dependency('rake')
|
165
|
-
s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
|
168
|
+
s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*', '.gemtest'].exclude('TODO').to_a
|
166
169
|
end
|
167
170
|
end
|
168
171
|
|
@@ -5,11 +5,11 @@ module Mocha
|
|
5
5
|
LIB_DIRECTORY = File.expand_path(File.join(File.dirname(__FILE__), "..")) + File::SEPARATOR
|
6
6
|
|
7
7
|
def initialize(lib_directory = LIB_DIRECTORY)
|
8
|
-
@
|
8
|
+
@path_pattern = Regexp.new(lib_directory)
|
9
9
|
end
|
10
10
|
|
11
11
|
def filtered(backtrace)
|
12
|
-
backtrace.reject { |location|
|
12
|
+
backtrace.reject { |location| @path_pattern.match(File.expand_path(location)) }
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
data/lib/mocha/expectation.rb
CHANGED
@@ -458,9 +458,9 @@ module Mocha # :nodoc:
|
|
458
458
|
message = "#{@cardinality.mocha_inspect}, "
|
459
459
|
message << case @invocation_count
|
460
460
|
when 0 then "not yet invoked"
|
461
|
-
when 1 then "
|
462
|
-
when 2 then "
|
463
|
-
else "
|
461
|
+
when 1 then "invoked once"
|
462
|
+
when 2 then "invoked twice"
|
463
|
+
else "invoked #{@invocation_count} times"
|
464
464
|
end
|
465
465
|
message << ": "
|
466
466
|
message << method_signature
|
@@ -6,7 +6,9 @@ if !MiniTest::Unit::TestCase.ancestors.include?(Mocha::API)
|
|
6
6
|
require 'mocha/integration/mini_test/version_13'
|
7
7
|
require 'mocha/integration/mini_test/version_140'
|
8
8
|
require 'mocha/integration/mini_test/version_141'
|
9
|
-
require 'mocha/integration/mini_test/
|
9
|
+
require 'mocha/integration/mini_test/version_142_to_172'
|
10
|
+
require 'mocha/integration/mini_test/version_200'
|
11
|
+
require 'mocha/integration/mini_test/version_201_to_202'
|
10
12
|
|
11
13
|
module MiniTest
|
12
14
|
class Unit
|
@@ -31,11 +33,15 @@ if !MiniTest::Unit::TestCase.ancestors.include?(Mocha::API)
|
|
31
33
|
include Mocha::Integration::MiniTest::Version140
|
32
34
|
elsif (mini_test_version == '1.4.1')
|
33
35
|
include Mocha::Integration::MiniTest::Version141
|
34
|
-
elsif (mini_test_version >= '1.4.2') && (mini_test_version <= '1.
|
35
|
-
include Mocha::Integration::MiniTest::
|
36
|
-
elsif (mini_test_version
|
36
|
+
elsif (mini_test_version >= '1.4.2') && (mini_test_version <= '1.7.2')
|
37
|
+
include Mocha::Integration::MiniTest::Version142To172
|
38
|
+
elsif (mini_test_version == '2.0.0')
|
39
|
+
include Mocha::Integration::MiniTest::Version200
|
40
|
+
elsif (mini_test_version >= '2.0.1') && (mini_test_version <= '2.0.2')
|
41
|
+
include Mocha::Integration::MiniTest::Version201To202
|
42
|
+
elsif (mini_test_version > '2.0.2')
|
37
43
|
$stderr.puts "*** MiniTest integration has not been verified but patching anyway ***" if $options['debug']
|
38
|
-
include Mocha::Integration::MiniTest::
|
44
|
+
include Mocha::Integration::MiniTest::Version201To202
|
39
45
|
else
|
40
46
|
$stderr.puts "*** No Mocha integration for MiniTest version ***" if $options['debug']
|
41
47
|
end
|
@@ -7,9 +7,9 @@ module Mocha
|
|
7
7
|
|
8
8
|
module MiniTest
|
9
9
|
|
10
|
-
module
|
10
|
+
module Version142To172
|
11
11
|
def self.included(mod)
|
12
|
-
$stderr.puts "Monkey patching MiniTest >= v1.4.2" if $options['debug']
|
12
|
+
$stderr.puts "Monkey patching MiniTest >= v1.4.2 and <= v1.7.2" if $options['debug']
|
13
13
|
end
|
14
14
|
def run runner
|
15
15
|
trap 'INFO' do
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'mocha/integration/mini_test/assertion_counter'
|
2
|
+
require 'mocha/expectation_error'
|
3
|
+
|
4
|
+
module Mocha
|
5
|
+
|
6
|
+
module Integration
|
7
|
+
|
8
|
+
module MiniTest
|
9
|
+
|
10
|
+
module Version200
|
11
|
+
def self.included(mod)
|
12
|
+
$stderr.puts "Monkey patching MiniTest v2.0.0" if $options['debug']
|
13
|
+
end
|
14
|
+
def run runner
|
15
|
+
trap 'INFO' do
|
16
|
+
time = Time.now - runner.start_time
|
17
|
+
warn "%s#%s %.2fs" % [self.class, self.__name__, time]
|
18
|
+
runner.status $stderr
|
19
|
+
end if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
|
20
|
+
|
21
|
+
assertion_counter = AssertionCounter.new(self)
|
22
|
+
result = ""
|
23
|
+
begin
|
24
|
+
begin
|
25
|
+
@passed = nil
|
26
|
+
self.setup
|
27
|
+
self.__send__ self.__name__
|
28
|
+
mocha_verify(assertion_counter)
|
29
|
+
result = "." unless io?
|
30
|
+
@passed = true
|
31
|
+
rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
|
32
|
+
raise
|
33
|
+
rescue Exception => e
|
34
|
+
@passed = false
|
35
|
+
result = runner.puke self.class, self.__name__, Mocha::Integration::MiniTest.translate(e)
|
36
|
+
ensure
|
37
|
+
begin
|
38
|
+
self.teardown
|
39
|
+
rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
|
40
|
+
raise
|
41
|
+
rescue Exception => e
|
42
|
+
result = runner.puke self.class, self.__name__, Mocha::Integration::MiniTest.translate(e)
|
43
|
+
end
|
44
|
+
trap 'INFO', 'DEFAULT' if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
|
45
|
+
end
|
46
|
+
ensure
|
47
|
+
mocha_teardown
|
48
|
+
end
|
49
|
+
result
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'mocha/integration/mini_test/assertion_counter'
|
2
|
+
require 'mocha/expectation_error'
|
3
|
+
|
4
|
+
module Mocha
|
5
|
+
|
6
|
+
module Integration
|
7
|
+
|
8
|
+
module MiniTest
|
9
|
+
|
10
|
+
module Version201To202
|
11
|
+
def self.included(mod)
|
12
|
+
$stderr.puts "Monkey patching MiniTest >= v2.0.1 <= v2.0.2" if $options['debug']
|
13
|
+
end
|
14
|
+
def run runner
|
15
|
+
trap 'INFO' do
|
16
|
+
time = runner.start_time ? Time.now - runner.start_time : 0
|
17
|
+
warn "%s#%s %.2fs" % [self.class, self.__name__, time]
|
18
|
+
runner.status $stderr
|
19
|
+
end if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
|
20
|
+
|
21
|
+
assertion_counter = AssertionCounter.new(self)
|
22
|
+
result = ""
|
23
|
+
begin
|
24
|
+
begin
|
25
|
+
@passed = nil
|
26
|
+
self.setup
|
27
|
+
self.__send__ self.__name__
|
28
|
+
mocha_verify(assertion_counter)
|
29
|
+
result = "." unless io?
|
30
|
+
@passed = true
|
31
|
+
rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
|
32
|
+
raise
|
33
|
+
rescue Exception => e
|
34
|
+
@passed = false
|
35
|
+
result = runner.puke self.class, self.__name__, Mocha::Integration::MiniTest.translate(e)
|
36
|
+
ensure
|
37
|
+
begin
|
38
|
+
self.teardown
|
39
|
+
rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
|
40
|
+
raise
|
41
|
+
rescue Exception => e
|
42
|
+
result = runner.puke self.class, self.__name__, Mocha::Integration::MiniTest.translate(e)
|
43
|
+
end
|
44
|
+
trap 'INFO', 'DEFAULT' if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
|
45
|
+
end
|
46
|
+
ensure
|
47
|
+
mocha_teardown
|
48
|
+
end
|
49
|
+
result
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/mocha/mock.rb
CHANGED
@@ -158,6 +158,7 @@ module Mocha # :nodoc:
|
|
158
158
|
matching_expectation_allowing_invocation.invoke(&block)
|
159
159
|
else
|
160
160
|
if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
|
161
|
+
matching_expectation.invoke(&block) if matching_expectation
|
161
162
|
message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
|
162
163
|
message << Mockery.instance.mocha_inspect
|
163
164
|
raise ExpectationError.new(message, caller)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'mocha/parameter_matchers/base'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Mocha
|
5
|
+
module ParameterMatchers
|
6
|
+
|
7
|
+
# :call-seq: has_equivalent_query_string(uri) -> parameter_matcher
|
8
|
+
#
|
9
|
+
# Matches a URI without regard to the ordering of parameters in the query string
|
10
|
+
# object = mock()
|
11
|
+
# object.expects(:method_1).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2))
|
12
|
+
# object.method_1('http://example.com/foo?b=2&a=1')
|
13
|
+
# # no error raised
|
14
|
+
#
|
15
|
+
# object = mock()
|
16
|
+
# object.expects(:method_1).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2))
|
17
|
+
# object.method_1('http://example.com/foo?a=1&b=3')
|
18
|
+
# # error raised, because the query parameters were different
|
19
|
+
def has_equivalent_query_string(uri)
|
20
|
+
QueryStringMatches.new(uri)
|
21
|
+
end
|
22
|
+
|
23
|
+
class QueryStringMatches < Base # :nodoc:
|
24
|
+
|
25
|
+
def initialize(uri)
|
26
|
+
@uri = URI.parse(uri)
|
27
|
+
end
|
28
|
+
|
29
|
+
def matches?(available_parameters)
|
30
|
+
actual = explode(URI.parse(available_parameters.shift))
|
31
|
+
expected = explode(@uri)
|
32
|
+
actual == expected
|
33
|
+
end
|
34
|
+
|
35
|
+
def mocha_inspect
|
36
|
+
"has_equivalent_query_string(#{@uri.mocha_inspect})"
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def explode(uri)
|
41
|
+
query_hash = (uri.query || '').split('&').inject({}){ |h, kv| h.merge(Hash[*kv.split('=')]) }
|
42
|
+
URI::Generic::COMPONENT.inject({}){ |h, k| h.merge(k => uri.__send__(k)) }.merge(:query => query_hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -23,8 +23,7 @@ class ExceptionRescueTest < Test::Unit::TestCase
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
assert_failed(test_result)
|
26
|
-
|
27
|
-
assert_equal "unexpected invocation: #<Mock:mock>.some_method()", failure_message_lines.first
|
26
|
+
assert_equal "unexpected invocation: #<Mock:mock>.some_method()", test_result.failure_message_lines[0]
|
28
27
|
end
|
29
28
|
|
30
29
|
def test_invocation_never_expected_exception_is_not_caught_by_standard_rescue
|
@@ -38,8 +37,7 @@ class ExceptionRescueTest < Test::Unit::TestCase
|
|
38
37
|
end
|
39
38
|
end
|
40
39
|
assert_failed(test_result)
|
41
|
-
|
42
|
-
assert_equal "unexpected invocation: #<Mock:mock>.some_method()", failure_message_lines.first
|
40
|
+
assert_equal "unexpected invocation: #<Mock:mock>.some_method()", test_result.failure_message_lines[0]
|
43
41
|
end
|
44
42
|
|
45
43
|
def test_unsatisfied_expectation_exception_is_not_caught_by_standard_rescue
|
@@ -48,11 +46,10 @@ class ExceptionRescueTest < Test::Unit::TestCase
|
|
48
46
|
mock.expects(:some_method)
|
49
47
|
end
|
50
48
|
assert_failed(test_result)
|
51
|
-
failure_message_lines = test_result.failure_messages.first.split("\n")
|
52
49
|
assert_equal [
|
53
50
|
"not all expectations were satisfied",
|
54
51
|
"unsatisfied expectations:",
|
55
52
|
"- expected exactly once, not yet invoked: #<Mock:mock>.some_method(any_parameters)"
|
56
|
-
], failure_message_lines
|
53
|
+
], test_result.failure_message_lines
|
57
54
|
end
|
58
55
|
end
|
@@ -29,7 +29,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
29
29
|
1.times { mock.method }
|
30
30
|
end
|
31
31
|
assert_failed(test_result)
|
32
|
-
assert_equal [
|
32
|
+
assert_equal [
|
33
|
+
"unexpected invocation: #<Mock:mock>.method()",
|
34
|
+
"unsatisfied expectations:",
|
35
|
+
"- expected never, invoked once: #<Mock:mock>.method(any_parameters)"
|
36
|
+
], test_result.failure_message_lines
|
33
37
|
end
|
34
38
|
|
35
39
|
def test_should_pass_if_method_is_expected_twice_and_is_called_twice
|
@@ -48,7 +52,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
48
52
|
1.times { mock.method }
|
49
53
|
end
|
50
54
|
assert_failed(test_result)
|
51
|
-
assert_equal [
|
55
|
+
assert_equal [
|
56
|
+
"not all expectations were satisfied",
|
57
|
+
"unsatisfied expectations:",
|
58
|
+
"- expected exactly twice, invoked once: #<Mock:mock>.method(any_parameters)"
|
59
|
+
], test_result.failure_message_lines
|
52
60
|
end
|
53
61
|
|
54
62
|
def test_should_fail_fast_if_method_is_expected_twice_but_is_called_three_times
|
@@ -58,7 +66,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
58
66
|
3.times { mock.method }
|
59
67
|
end
|
60
68
|
assert_failed(test_result)
|
61
|
-
assert_equal [
|
69
|
+
assert_equal [
|
70
|
+
"unexpected invocation: #<Mock:mock>.method()",
|
71
|
+
"unsatisfied expectations:",
|
72
|
+
"- expected exactly twice, invoked 3 times: #<Mock:mock>.method(any_parameters)"
|
73
|
+
], test_result.failure_message_lines
|
62
74
|
end
|
63
75
|
|
64
76
|
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_twice
|
@@ -95,7 +107,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
95
107
|
1.times { mock.method }
|
96
108
|
end
|
97
109
|
assert_failed(test_result)
|
98
|
-
assert_equal [
|
110
|
+
assert_equal [
|
111
|
+
"not all expectations were satisfied",
|
112
|
+
"unsatisfied expectations:",
|
113
|
+
"- expected between 2 and 4 times, invoked once: #<Mock:mock>.method(any_parameters)"
|
114
|
+
], test_result.failure_message_lines
|
99
115
|
end
|
100
116
|
|
101
117
|
def test_should_fail_fast_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
|
@@ -105,7 +121,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
105
121
|
5.times { mock.method }
|
106
122
|
end
|
107
123
|
assert_failed(test_result)
|
108
|
-
assert_equal [
|
124
|
+
assert_equal [
|
125
|
+
"unexpected invocation: #<Mock:mock>.method()",
|
126
|
+
"unsatisfied expectations:",
|
127
|
+
"- expected between 2 and 4 times, invoked 5 times: #<Mock:mock>.method(any_parameters)"
|
128
|
+
], test_result.failure_message_lines
|
109
129
|
end
|
110
130
|
|
111
131
|
def test_should_pass_if_method_is_expected_at_least_once_and_is_called_once
|
@@ -133,7 +153,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
133
153
|
0.times { mock.method }
|
134
154
|
end
|
135
155
|
assert_failed(test_result)
|
136
|
-
assert_equal [
|
156
|
+
assert_equal [
|
157
|
+
"not all expectations were satisfied",
|
158
|
+
"unsatisfied expectations:",
|
159
|
+
"- expected at least once, not yet invoked: #<Mock:mock>.method(any_parameters)"
|
160
|
+
], test_result.failure_message_lines
|
137
161
|
end
|
138
162
|
|
139
163
|
def test_should_pass_if_method_is_expected_at_most_once_and_is_never_called
|
@@ -161,7 +185,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
161
185
|
2.times { mock.method }
|
162
186
|
end
|
163
187
|
assert_failed(test_result)
|
164
|
-
assert_equal [
|
188
|
+
assert_equal [
|
189
|
+
"unexpected invocation: #<Mock:mock>.method()",
|
190
|
+
"unsatisfied expectations:",
|
191
|
+
"- expected at most once, invoked twice: #<Mock:mock>.method(any_parameters)"
|
192
|
+
], test_result.failure_message_lines
|
165
193
|
end
|
166
194
|
|
167
195
|
def test_should_pass_if_method_is_never_expected_and_is_never_called_even_if_everything_is_stubbed
|
@@ -180,7 +208,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
180
208
|
1.times { stub.method }
|
181
209
|
end
|
182
210
|
assert_failed(test_result)
|
183
|
-
assert_equal [
|
211
|
+
assert_equal [
|
212
|
+
"unexpected invocation: #<Mock:stub>.method()",
|
213
|
+
"unsatisfied expectations:",
|
214
|
+
"- expected never, invoked once: #<Mock:stub>.method(any_parameters)"
|
215
|
+
], test_result.failure_message_lines
|
184
216
|
end
|
185
217
|
|
186
218
|
def test_should_fail_fast_if_there_is_no_matching_expectation
|
@@ -190,7 +222,11 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
|
|
190
222
|
1.times { mock.method }
|
191
223
|
end
|
192
224
|
assert_failed(test_result)
|
193
|
-
assert_equal [
|
225
|
+
assert_equal [
|
226
|
+
"unexpected invocation: #<Mock:mock>.method()",
|
227
|
+
"unsatisfied expectations:",
|
228
|
+
"- expected exactly once, not yet invoked: #<Mock:mock>.method(1)"
|
229
|
+
], test_result.failure_message_lines
|
194
230
|
end
|
195
231
|
|
196
232
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class GithubIssue20Test < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include AcceptanceTest
|
7
|
+
|
8
|
+
def setup
|
9
|
+
setup_acceptance_test
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
teardown_acceptance_test
|
14
|
+
end
|
15
|
+
|
16
|
+
class MyClass
|
17
|
+
def my_instance_method
|
18
|
+
:original_value
|
19
|
+
end
|
20
|
+
# class << self
|
21
|
+
# attr_accessor :my_instance
|
22
|
+
# end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_me
|
26
|
+
# MyClass.my_instance = MyClass.new
|
27
|
+
test_case = Class.new(Test::Unit::TestCase) do
|
28
|
+
def setup
|
29
|
+
@my_instance = MyClass.new
|
30
|
+
end
|
31
|
+
def test_1
|
32
|
+
@my_instance.stubs(:my_instance_method).returns(:first_value)
|
33
|
+
assert_equal :first_value, @my_instance.my_instance_method
|
34
|
+
end
|
35
|
+
def test_2
|
36
|
+
assert_equal :original_value, @my_instance.my_instance_method
|
37
|
+
MyClass.any_instance.stubs(:my_instance_method).returns(:second_value)
|
38
|
+
assert_equal :second_value, @my_instance.my_instance_method
|
39
|
+
end
|
40
|
+
end
|
41
|
+
test_result = run_test_case(test_case)
|
42
|
+
assert_passed(test_result)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class FailureMessageTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include AcceptanceTest
|
7
|
+
|
8
|
+
def setup
|
9
|
+
setup_acceptance_test
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
teardown_acceptance_test
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_include_unexpected_invocation_in_unsatisfied_expectation_message
|
17
|
+
test_result = run_as_test do
|
18
|
+
mock = mock('mock')
|
19
|
+
mock.expects(:method_one).once
|
20
|
+
2.times { mock.method_one }
|
21
|
+
end
|
22
|
+
assert_failed(test_result)
|
23
|
+
assert_equal [
|
24
|
+
"unexpected invocation: #<Mock:mock>.method_one()",
|
25
|
+
"unsatisfied expectations:",
|
26
|
+
"- expected exactly once, invoked twice: #<Mock:mock>.method_one(any_parameters)"
|
27
|
+
], test_result.failure_message_lines
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_report_satisfied_expectations_as_well_as_unsatisfied_expectations
|
31
|
+
test_result = run_as_test do
|
32
|
+
mock = mock('mock')
|
33
|
+
mock.expects(:method_one).once
|
34
|
+
mock.expects(:method_two).twice
|
35
|
+
1.times { mock.method_one }
|
36
|
+
1.times { mock.method_two }
|
37
|
+
end
|
38
|
+
assert_failed(test_result)
|
39
|
+
assert_equal [
|
40
|
+
"not all expectations were satisfied",
|
41
|
+
"unsatisfied expectations:",
|
42
|
+
"- expected exactly twice, invoked once: #<Mock:mock>.method_two(any_parameters)",
|
43
|
+
"satisfied expectations:",
|
44
|
+
"- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)"
|
45
|
+
], test_result.failure_message_lines
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_report_multiple_satisfied_expectations
|
49
|
+
test_result = run_as_test do
|
50
|
+
mock = mock('mock')
|
51
|
+
mock.expects(:method_one).once
|
52
|
+
mock.expects(:method_two).twice
|
53
|
+
mock.expects(:method_three).times(3)
|
54
|
+
1.times { mock.method_one }
|
55
|
+
2.times { mock.method_two }
|
56
|
+
2.times { mock.method_three }
|
57
|
+
end
|
58
|
+
assert_failed(test_result)
|
59
|
+
assert_equal [
|
60
|
+
"not all expectations were satisfied",
|
61
|
+
"unsatisfied expectations:",
|
62
|
+
"- expected exactly 3 times, invoked twice: #<Mock:mock>.method_three(any_parameters)",
|
63
|
+
"satisfied expectations:",
|
64
|
+
"- expected exactly twice, invoked twice: #<Mock:mock>.method_two(any_parameters)",
|
65
|
+
"- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)"
|
66
|
+
], test_result.failure_message_lines
|
67
|
+
end
|
68
|
+
end
|
@@ -243,4 +243,58 @@ class ParameterMatcherTest < Test::Unit::TestCase
|
|
243
243
|
assert_failed(test_result)
|
244
244
|
end
|
245
245
|
|
246
|
+
def test_should_match_parameter_that_has_identical_query_string
|
247
|
+
test_result = run_as_test do
|
248
|
+
mock = mock()
|
249
|
+
mock.expects(:method).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2'))
|
250
|
+
mock.method('http://example.com/foo?a=1&b=2')
|
251
|
+
end
|
252
|
+
assert_passed(test_result)
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_should_match_parameter_that_has_rearranged_query_string
|
256
|
+
test_result = run_as_test do
|
257
|
+
mock = mock()
|
258
|
+
mock.expects(:method).with(has_equivalent_query_string('http://example.com/foo?b=2&a=1'))
|
259
|
+
mock.method('http://example.com/foo?a=1&b=2')
|
260
|
+
end
|
261
|
+
assert_passed(test_result)
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_should_not_match_parameter_that_does_not_have_the_same_query_parameters
|
265
|
+
test_result = run_as_test do
|
266
|
+
mock = mock()
|
267
|
+
mock.expects(:method).with(has_equivalent_query_string('http://example.com/foo?a=1'))
|
268
|
+
mock.method('http://example.com/foo?a=1&b=2')
|
269
|
+
end
|
270
|
+
assert_failed(test_result)
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_should_not_match_parameter_that_has_no_query_parameters_when_they_are_expected
|
274
|
+
test_result = run_as_test do
|
275
|
+
mock = mock()
|
276
|
+
mock.expects(:method).with(has_equivalent_query_string('http://example.com/foo'))
|
277
|
+
mock.method('http://example.com/foo?a=1&b=2')
|
278
|
+
end
|
279
|
+
assert_failed(test_result)
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_should_not_match_parameter_that_has_the_same_query_string_bit_which_differs_otherwise
|
283
|
+
test_result = run_as_test do
|
284
|
+
mock = mock()
|
285
|
+
mock.expects(:method).with(has_equivalent_query_string('http://a.example.com/foo?a=1&b=2'))
|
286
|
+
mock.method('http://b.example.com/foo?a=1&b=2')
|
287
|
+
end
|
288
|
+
assert_failed(test_result)
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_should_match_parameter_with_no_domain_or_scheme
|
292
|
+
test_result = run_as_test do
|
293
|
+
mock = mock()
|
294
|
+
mock.expects(:method).with(has_equivalent_query_string('/foo?a=1&b=2'))
|
295
|
+
mock.method('/foo?a=1&b=2')
|
296
|
+
end
|
297
|
+
assert_passed(test_result)
|
298
|
+
end
|
299
|
+
|
246
300
|
end
|
data/test/test_runner.rb
CHANGED
@@ -8,11 +8,8 @@ else
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module TestRunner
|
11
|
-
def
|
12
|
-
|
13
|
-
define_method(:test_me, &block)
|
14
|
-
end
|
15
|
-
test = test_class.new(:test_me)
|
11
|
+
def run_test_case(test_class, test_result = nil)
|
12
|
+
test = test_class.suite
|
16
13
|
|
17
14
|
if defined?(Test::Unit::TestResult)
|
18
15
|
test_result ||= Test::Unit::TestResult.new
|
@@ -22,6 +19,9 @@ module TestRunner
|
|
22
19
|
def failure_messages
|
23
20
|
failures.map { |failure| failure.message }
|
24
21
|
end
|
22
|
+
def failure_message_lines
|
23
|
+
failure_messages.map { |message| message.split("\n") }.flatten
|
24
|
+
end
|
25
25
|
def error_messages
|
26
26
|
errors.map { |error| error.message }
|
27
27
|
end
|
@@ -35,6 +35,13 @@ module TestRunner
|
|
35
35
|
test_result
|
36
36
|
end
|
37
37
|
|
38
|
+
def run_as_test(test_result = nil, &block)
|
39
|
+
test_class = Class.new(Test::Unit::TestCase) do
|
40
|
+
define_method(:test_me, &block)
|
41
|
+
end
|
42
|
+
run_test_case(test_class)
|
43
|
+
end
|
44
|
+
|
38
45
|
def assert_passed(test_result)
|
39
46
|
flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
|
40
47
|
flunk "Test failed unexpectedly with message: #{test_result.errors}" if test_result.error_count > 0
|
@@ -296,7 +296,7 @@ class ExpectationTest < Test::Unit::TestCase
|
|
296
296
|
expectation = new_expectation.times(2)
|
297
297
|
1.times {expectation.invoke}
|
298
298
|
assert !expectation.verified?
|
299
|
-
assert_match(/expected exactly twice,
|
299
|
+
assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
|
300
300
|
end
|
301
301
|
|
302
302
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mocha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 45
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 11
|
10
|
+
version: 0.9.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- James Mead
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-03 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -64,7 +64,9 @@ files:
|
|
64
64
|
- lib/mocha/integration/mini_test/version_13.rb
|
65
65
|
- lib/mocha/integration/mini_test/version_140.rb
|
66
66
|
- lib/mocha/integration/mini_test/version_141.rb
|
67
|
-
- lib/mocha/integration/mini_test/
|
67
|
+
- lib/mocha/integration/mini_test/version_142_to_172.rb
|
68
|
+
- lib/mocha/integration/mini_test/version_200.rb
|
69
|
+
- lib/mocha/integration/mini_test/version_201_to_202.rb
|
68
70
|
- lib/mocha/integration/mini_test.rb
|
69
71
|
- lib/mocha/integration/test_unit/assertion_counter.rb
|
70
72
|
- lib/mocha/integration/test_unit/gem_version_200.rb
|
@@ -103,6 +105,7 @@ files:
|
|
103
105
|
- lib/mocha/parameter_matchers/not.rb
|
104
106
|
- lib/mocha/parameter_matchers/object.rb
|
105
107
|
- lib/mocha/parameter_matchers/optionally.rb
|
108
|
+
- lib/mocha/parameter_matchers/query_string.rb
|
106
109
|
- lib/mocha/parameter_matchers/regexp_matches.rb
|
107
110
|
- lib/mocha/parameter_matchers/responds_with.rb
|
108
111
|
- lib/mocha/parameter_matchers/yaml_equivalent.rb
|
@@ -129,12 +132,14 @@ files:
|
|
129
132
|
- test/acceptance/exception_rescue_test.rb
|
130
133
|
- test/acceptance/expected_invocation_count_test.rb
|
131
134
|
- test/acceptance/failure_messages_test.rb
|
135
|
+
- test/acceptance/github_issue_20_test.rb
|
132
136
|
- test/acceptance/minitest_test.rb
|
133
137
|
- test/acceptance/mocha_example_test.rb
|
134
138
|
- test/acceptance/mocha_test_result_test.rb
|
135
139
|
- test/acceptance/mock_test.rb
|
136
140
|
- test/acceptance/mock_with_initializer_block_test.rb
|
137
141
|
- test/acceptance/mocked_methods_dispatch_test.rb
|
142
|
+
- test/acceptance/multiple_expectations_failure_message_test.rb
|
138
143
|
- test/acceptance/optional_parameters_test.rb
|
139
144
|
- test/acceptance/parameter_matcher_test.rb
|
140
145
|
- test/acceptance/partial_mocks_test.rb
|
@@ -222,6 +227,7 @@ files:
|
|
222
227
|
- Rakefile
|
223
228
|
- README.rdoc
|
224
229
|
- RELEASE.rdoc
|
230
|
+
- .gemtest
|
225
231
|
has_rdoc: true
|
226
232
|
homepage: http://mocha.rubyforge.org
|
227
233
|
licenses: []
|