jferris-mocha 0.9.5.0.1240002286
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +3 -0
- data/MIT-LICENSE +7 -0
- data/README +37 -0
- data/RELEASE +269 -0
- data/Rakefile +217 -0
- data/examples/misc.rb +43 -0
- data/examples/mocha.rb +25 -0
- data/examples/stubba.rb +64 -0
- data/lib/mocha/any_instance_method.rb +55 -0
- data/lib/mocha/api.rb +232 -0
- data/lib/mocha/argument_iterator.rb +21 -0
- data/lib/mocha/backtrace_filter.rb +17 -0
- data/lib/mocha/cardinality.rb +95 -0
- data/lib/mocha/central.rb +27 -0
- data/lib/mocha/change_state_side_effect.rb +19 -0
- data/lib/mocha/class_method.rb +87 -0
- data/lib/mocha/configuration.rb +60 -0
- data/lib/mocha/deprecation.rb +22 -0
- data/lib/mocha/exception_raiser.rb +17 -0
- data/lib/mocha/expectation.rb +484 -0
- data/lib/mocha/expectation_error.rb +15 -0
- data/lib/mocha/expectation_list.rb +50 -0
- data/lib/mocha/in_state_ordering_constraint.rb +19 -0
- data/lib/mocha/inspect.rb +67 -0
- data/lib/mocha/instance_method.rb +16 -0
- data/lib/mocha/integration/bacon/assertion_counter.rb +23 -0
- data/lib/mocha/integration/bacon/version_11_and_above.rb +34 -0
- data/lib/mocha/integration/bacon.rb +1 -0
- data/lib/mocha/integration/mini_test/assertion_counter.rb +23 -0
- data/lib/mocha/integration/mini_test/version_131_and_above.rb +49 -0
- data/lib/mocha/integration/mini_test.rb +1 -0
- data/lib/mocha/integration/test_unit/assertion_counter.rb +23 -0
- data/lib/mocha/integration/test_unit/gem_version_200.rb +49 -0
- data/lib/mocha/integration/test_unit/gem_version_201_and_above.rb +49 -0
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +48 -0
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +50 -0
- data/lib/mocha/integration/test_unit.rb +4 -0
- data/lib/mocha/invocation.rb +10 -0
- data/lib/mocha/is_a.rb +9 -0
- data/lib/mocha/logger.rb +15 -0
- data/lib/mocha/metaclass.rb +13 -0
- data/lib/mocha/method_matcher.rb +21 -0
- data/lib/mocha/mock.rb +200 -0
- data/lib/mocha/mockery.rb +189 -0
- data/lib/mocha/module_method.rb +16 -0
- data/lib/mocha/multiple_yields.rb +20 -0
- data/lib/mocha/names.rb +53 -0
- data/lib/mocha/no_yields.rb +11 -0
- data/lib/mocha/object.rb +187 -0
- data/lib/mocha/parameter_matchers/all_of.rb +42 -0
- data/lib/mocha/parameter_matchers/any_of.rb +47 -0
- data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
- data/lib/mocha/parameter_matchers/anything.rb +33 -0
- data/lib/mocha/parameter_matchers/base.rb +15 -0
- data/lib/mocha/parameter_matchers/equals.rb +42 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +45 -0
- data/lib/mocha/parameter_matchers/has_entry.rb +57 -0
- data/lib/mocha/parameter_matchers/has_key.rb +42 -0
- data/lib/mocha/parameter_matchers/has_value.rb +42 -0
- data/lib/mocha/parameter_matchers/includes.rb +40 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +42 -0
- data/lib/mocha/parameter_matchers/is_a.rb +42 -0
- data/lib/mocha/parameter_matchers/kind_of.rb +42 -0
- data/lib/mocha/parameter_matchers/not.rb +42 -0
- data/lib/mocha/parameter_matchers/object.rb +15 -0
- data/lib/mocha/parameter_matchers/optionally.rb +55 -0
- data/lib/mocha/parameter_matchers/regexp_matches.rb +43 -0
- data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
- data/lib/mocha/parameter_matchers.rb +27 -0
- data/lib/mocha/parameters_matcher.rb +37 -0
- data/lib/mocha/pretty_parameters.rb +28 -0
- data/lib/mocha/return_values.rb +31 -0
- data/lib/mocha/sequence.rb +42 -0
- data/lib/mocha/single_return_value.rb +17 -0
- data/lib/mocha/single_yield.rb +18 -0
- data/lib/mocha/state_machine.rb +91 -0
- data/lib/mocha/stubbing_error.rb +16 -0
- data/lib/mocha/unexpected_invocation.rb +18 -0
- data/lib/mocha/yield_parameters.rb +31 -0
- data/lib/mocha.rb +69 -0
- data/lib/mocha_standalone.rb +2 -0
- data/lib/stubba.rb +4 -0
- data/test/acceptance/acceptance_test_helper.rb +38 -0
- data/test/acceptance/api_test.rb +139 -0
- data/test/acceptance/bacon_spec.rb +67 -0
- data/test/acceptance/bacon_test.rb +110 -0
- data/test/acceptance/bug_18914_test.rb +43 -0
- data/test/acceptance/bug_21465_test.rb +34 -0
- data/test/acceptance/bug_21563_test.rb +25 -0
- data/test/acceptance/expected_invocation_count_test.rb +196 -0
- data/test/acceptance/failure_messages_test.rb +64 -0
- data/test/acceptance/minitest_test.rb +146 -0
- data/test/acceptance/mocha_example_test.rb +98 -0
- data/test/acceptance/mocha_test_result_test.rb +84 -0
- data/test/acceptance/mock_test.rb +100 -0
- data/test/acceptance/mock_with_initializer_block_test.rb +51 -0
- data/test/acceptance/mocked_methods_dispatch_test.rb +78 -0
- data/test/acceptance/optional_parameters_test.rb +70 -0
- data/test/acceptance/parameter_matcher_test.rb +209 -0
- data/test/acceptance/partial_mocks_test.rb +47 -0
- data/test/acceptance/return_value_test.rb +52 -0
- data/test/acceptance/sequence_test.rb +186 -0
- data/test/acceptance/spy_test.rb +109 -0
- data/test/acceptance/states_test.rb +70 -0
- data/test/acceptance/stub_any_instance_method_test.rb +195 -0
- data/test/acceptance/stub_class_method_test.rb +203 -0
- data/test/acceptance/stub_everything_test.rb +56 -0
- data/test/acceptance/stub_instance_method_test.rb +203 -0
- data/test/acceptance/stub_module_method_test.rb +163 -0
- data/test/acceptance/stub_test.rb +52 -0
- data/test/acceptance/stubba_example_test.rb +102 -0
- data/test/acceptance/stubba_test.rb +15 -0
- data/test/acceptance/stubba_test_result_test.rb +66 -0
- data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +157 -0
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +147 -0
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
- data/test/acceptance/stubbing_non_public_class_method_test.rb +163 -0
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +143 -0
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
- data/test/deprecation_disabler.rb +15 -0
- data/test/execution_point.rb +36 -0
- data/test/matcher_helpers.rb +5 -0
- data/test/method_definer.rb +24 -0
- data/test/simple_counter.rb +13 -0
- data/test/test_helper.rb +16 -0
- data/test/test_runner.rb +33 -0
- data/test/unit/any_instance_method_test.rb +126 -0
- data/test/unit/array_inspect_test.rb +16 -0
- data/test/unit/assert_received_test.rb +136 -0
- data/test/unit/backtrace_filter_test.rb +19 -0
- data/test/unit/cardinality_test.rb +56 -0
- data/test/unit/central_test.rb +65 -0
- data/test/unit/change_state_side_effect_test.rb +41 -0
- data/test/unit/class_method_test.rb +237 -0
- data/test/unit/date_time_inspect_test.rb +21 -0
- data/test/unit/exception_raiser_test.rb +42 -0
- data/test/unit/expectation_list_test.rb +57 -0
- data/test/unit/expectation_test.rb +526 -0
- data/test/unit/hash_inspect_test.rb +16 -0
- data/test/unit/have_received_test.rb +137 -0
- data/test/unit/in_state_ordering_constraint_test.rb +43 -0
- data/test/unit/invocation_test.rb +17 -0
- data/test/unit/metaclass_test.rb +22 -0
- data/test/unit/method_matcher_test.rb +23 -0
- data/test/unit/mock_test.rb +329 -0
- data/test/unit/mockery_test.rb +163 -0
- data/test/unit/multiple_yields_test.rb +18 -0
- data/test/unit/no_yields_test.rb +18 -0
- data/test/unit/object_inspect_test.rb +37 -0
- data/test/unit/object_test.rb +82 -0
- data/test/unit/parameter_matchers/all_of_test.rb +26 -0
- data/test/unit/parameter_matchers/any_of_test.rb +26 -0
- data/test/unit/parameter_matchers/anything_test.rb +21 -0
- data/test/unit/parameter_matchers/equals_test.rb +25 -0
- data/test/unit/parameter_matchers/has_entries_test.rb +51 -0
- data/test/unit/parameter_matchers/has_entry_test.rb +82 -0
- data/test/unit/parameter_matchers/has_key_test.rb +36 -0
- data/test/unit/parameter_matchers/has_value_test.rb +37 -0
- data/test/unit/parameter_matchers/includes_test.rb +25 -0
- data/test/unit/parameter_matchers/instance_of_test.rb +25 -0
- data/test/unit/parameter_matchers/is_a_test.rb +25 -0
- data/test/unit/parameter_matchers/kind_of_test.rb +25 -0
- data/test/unit/parameter_matchers/not_test.rb +26 -0
- data/test/unit/parameter_matchers/regexp_matches_test.rb +25 -0
- data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
- data/test/unit/parameter_matchers/stub_matcher.rb +27 -0
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
- data/test/unit/parameters_matcher_test.rb +121 -0
- data/test/unit/return_values_test.rb +63 -0
- data/test/unit/sequence_test.rb +104 -0
- data/test/unit/single_return_value_test.rb +14 -0
- data/test/unit/single_yield_test.rb +18 -0
- data/test/unit/state_machine_test.rb +98 -0
- data/test/unit/string_inspect_test.rb +11 -0
- data/test/unit/yield_parameters_test.rb +93 -0
- metadata +245 -0
data/COPYING
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2006 Revieworld Ltd.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= Mocha
|
2
|
+
|
3
|
+
Mocha is a library for mocking and stubbing using a syntax like that of JMock[http://www.jmock.org].
|
4
|
+
|
5
|
+
It can be used with many testing frameworks e.g. Test::Unit[http://www.ruby-doc.org/core/classes/Test/Unit.html], RSpec[http://rspec.info/], test/spec[http://chneukirchen.org/repos/testspec/README], expectations[http://expectations.rubyforge.org/], Dust[http://dust.rubyforge.org/], MiniTest[http://rubyforge.org/projects/bfts] and even JtestR[http://jtestr.codehaus.org/].
|
6
|
+
|
7
|
+
Mocha provides a unified, simple and readable syntax for both traditional mocking and partial mocking.
|
8
|
+
|
9
|
+
Mocha was harvested from projects at Reevoo[http://www.reevoo.com] by me (James[http://blog.floehopper.org]) and my (then) colleagues Ben[http://www.techbelly.com/], Chris[http://chrisroos.co.uk] and Paul[http://po-ru.com].
|
10
|
+
|
11
|
+
== Download and Installation
|
12
|
+
|
13
|
+
Install the gem with the following command...
|
14
|
+
|
15
|
+
$ gem install mocha
|
16
|
+
|
17
|
+
Or install the Rails[http://www.rubyonrails.org] plugin...
|
18
|
+
|
19
|
+
$ script/plugin install git://github.com/floehopper/mocha.git
|
20
|
+
|
21
|
+
Or download Mocha...
|
22
|
+
|
23
|
+
http://rubyforge.org/frs/?group_id=1917
|
24
|
+
|
25
|
+
== Examples
|
26
|
+
|
27
|
+
* Quick Start - {Usage Examples}[link:examples/misc.html]
|
28
|
+
* Traditional mocking - {Star Trek Example}[link:examples/mocha.html]
|
29
|
+
* Setting expectations on real classes - {Order Example}[link:examples/stubba.html]
|
30
|
+
* More examples on {Floehopper's Blog}[http://blog.floehopper.org]
|
31
|
+
* {Mailing List Archives}[http://groups.google.com/group/mocha-developer]
|
32
|
+
|
33
|
+
== License
|
34
|
+
|
35
|
+
Copyright Revieworld Ltd. 2006
|
36
|
+
|
37
|
+
You may use, copy and redistribute this library under the same terms as {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the {MIT license}[http://mocha.rubyforge.org/files/MIT-LICENSE.html].
|
data/RELEASE
ADDED
@@ -0,0 +1,269 @@
|
|
1
|
+
= 0.9.5 (93cad010345ce5d68f31422cfc32ed9dd6de13ec)
|
2
|
+
* Fixed Lighthouse bug #32 - stub_everything should mean mock responds to anything.
|
3
|
+
* Added Expectation#twice to improve readability. Thanks to pull request from Celestino Gomes.
|
4
|
+
* In Ruby 1.9.1, requiring 'test/unit' loads a thin wrapper around MiniTest and Test::Unit::TestCase ends up inheriting from MiniTest::Unit::TestCase. So we need to avoid including the Mocha modules more than once to avoid nasty consequences. Thanks to Matthias Hennemeyer for help with this.
|
5
|
+
* Ruby 1.9 includes rake, but not rake/contrib. For the moment I've moved the sshpublisher require into the only rake task that needs it, so that I can at least run the tests in Ruby 1.9. It looks like I will need to build a rake/contrib gem or similar to get this working properly - http://intertwingly.net/blog/2008/01/07/Rake-Contrib-for-1-9
|
6
|
+
|
7
|
+
= 0.9.4 (8a59c6ff0f99f34b02bd99f19536a7893be2b340)
|
8
|
+
* Added mocha.gemspec file generated with Chad Woolley's new rake task, so that a floehopper-mocha gem will get built on GitHub.
|
9
|
+
* Add rake task to update mocha.gemspec with unique version, which will cause gem to be auto-built on github
|
10
|
+
* As Tobias Crawley correctly pointed out in feature request #23055 "stubs(with_hash) not working with existing object" [1], following the principle of least surprise, it should be possible to call ObjectMethods#expects & ObjectMethods#stubs with a Hash of method_names vs return_values like you can with Mock#expects & Mock#stubs. I've also updated & improved the docs to reflect the changes. [1] http://rubyforge.org/tracker/index.php?func=detail&aid=23055&group_id=1917&atid=7480
|
11
|
+
* Removed deprecated gem autorequire.
|
12
|
+
|
13
|
+
= 0.9.3 (8219bb2d2881c8529c93fc21e97a11d01203c759)
|
14
|
+
* Added support for MiniTest thanks to Jeff Smick.
|
15
|
+
* Fixed a possible bug with some of the non-default Configuration options relating to the argument to Object#respond_to?
|
16
|
+
* As per Jay Fields recommendations [1] and with further impetus from a talk at Ruby Manor, any methods added to core classes are now added by including a module. This means that Mocha is a better citizen of the Ruby world and it's behaviour is more easily extended. [1] http://blog.jayfields.com/2008/07/ruby-underuse-of-modules.html & http://blog.jayfields.com/2008/07/ruby-redefine-method-behavior.html
|
17
|
+
* Removed deprecated gem autorequire.
|
18
|
+
|
19
|
+
= 0.9.2 (r355)
|
20
|
+
* Improved documentation to address [#22530] 'Mock methods with multiple return values not possible?'
|
21
|
+
* respond_with parameter matcher was not available in tests.
|
22
|
+
* Patch [#22630] Fix for a bug in running Rails tests with Ruby 1.8.7. Array#flatten was being called which in turn was checking whether each element responded to #to_ary. This check was using the two parameter version of #respond_to?, but Mock was only defining a one parameter version.
|
23
|
+
|
24
|
+
= 0.9.1 (r349)
|
25
|
+
|
26
|
+
* Fixed bug #21465 - expects & stubs should support method names as strings (as well as symbols) or fail fast. Convert all expectation method names to a symbol in case they were supplied as a string.
|
27
|
+
* By removing Mock#unexpected_method_called we reduce the number of methods vulnerable to the problem that surfaced in bug #21563.
|
28
|
+
* Fix bug #21563 - stubbing 'verified?' method is unsafe. Instance method names on the Mock class should be more obscure.
|
29
|
+
* Performance improvement. StubbaExampleTest goes twice as fast on my local machine.
|
30
|
+
* Added primitive performance test to default rake task.
|
31
|
+
* Fix format of case statements which don't work in Ruby 1.9 and make others consistent.
|
32
|
+
* There is no point in running (potentially expensive) checks if configuration is set to allow such checks to fail. This is a relatively quick fix in response to Chris McGrath's performance problems.
|
33
|
+
* Fix for bug #21161 - 'uninitialized constant Deprecation in stubba.rb'.
|
34
|
+
* It's more readable to talk about 'once' and 'twice' rather than '1 time' and '2 times'.
|
35
|
+
* Fix bug #20883 - never should raise when called to prevent follow up errors. Fail fast when there are no matching invokable expectations and handle the stub_everything case sensibly. This might not be entirely backwards compatible, but I think the benefits outweigh the risks. The most likely change is that a test that was already failing will now fail faster, which doesn't seem so awful.
|
36
|
+
|
37
|
+
= 0.9.0 (r316)
|
38
|
+
|
39
|
+
* Configurable warnings or errors
|
40
|
+
* when a method on a non-public method is stubbed
|
41
|
+
* when a method on a non-existent method is stubbed
|
42
|
+
* when a method on a non-mock object is stubbed
|
43
|
+
* when a method is stubbed unnecessarily (i.e. the stubbed method is not called during the test)
|
44
|
+
|
45
|
+
* Improved error messages
|
46
|
+
* User-friendly list of unsatisfied expectations, satisfied expectations and state machines.
|
47
|
+
* Improved readability of cardinality description.
|
48
|
+
* Display sensible failure message for any_instance expectations e.g. "#<AnyInstance:Foo>.bar - expected calls: 1, actual calls: 0"
|
49
|
+
|
50
|
+
* Parameter matchers
|
51
|
+
* New to this release
|
52
|
+
* optionally (allows matching of optional parameters if available)
|
53
|
+
* yaml_equivalent (allows matching of YAML that represents the specified object)
|
54
|
+
* responds_with (tests the quack not the duck)
|
55
|
+
* Nesting of parameter matchers is now supported.
|
56
|
+
|
57
|
+
* Optional block passed into mock initializer is evaluated in the context of the new mock instance and can be used as a shortcut to set up expectations.
|
58
|
+
|
59
|
+
* Added JMock-style sequences for constraining the order of expected invocations. See Standalone#sequence and Expectation#in_sequence.
|
60
|
+
|
61
|
+
* Added JMock-style states for constraining the order of expected invocations. See Standalone#states, Expectation#then, Expectation#when and StateMachine.
|
62
|
+
|
63
|
+
* Compatibility with versions of Ruby
|
64
|
+
* Compatibility with Ruby v1.9. All test errors and warnings fixed.
|
65
|
+
* Nasty fix so that TestCaseAdaptor works consistently with earlier versions of Test::Unit as well as more recent versions.
|
66
|
+
* Added platform to gem specification to avoid bug in rubygems 0.9.5 - see http://www.dcmanges.com/blog/rubygems-0-9-5-platform-bug and http://rubygems.org/read/chapter/20#platform.
|
67
|
+
* Make ExpectationRaiser deal with subclasses of Interrupt which seem to need a message supplied in the raise statement in Ruby 1.8.6 (but not 1.8.4 or 1.9). Not sure this is really Mocha's responsibility.
|
68
|
+
|
69
|
+
* Added deprecation warning in stubba.rb which is no longer needed and will be removed.
|
70
|
+
|
71
|
+
* Supply positioning information to evals to improve any error messages. See http://ola-bini.blogspot.com/2008/01/ruby-antipattern-using-eval-without.html
|
72
|
+
|
73
|
+
* Bug fixes
|
74
|
+
* 18914 in revision 296 - http://rubyforge.org/tracker/index.php?func=detail&aid=18914&group_id=1917&atid=7477
|
75
|
+
* 18917 in revision 295 - http://rubyforge.org/tracker/index.php?func=detail&aid=18917&group_id=1917&atid=7477
|
76
|
+
* 18336 in revision 287 - http://rubyforge.org/tracker/index.php?func=detail&aid=18336&group_id=1917&atid=7477
|
77
|
+
* 17835 in revision 255 - http://rubyforge.org/tracker/index.php?func=detail&aid=17835&group_id=1917&atid=7477
|
78
|
+
* 17412 in revision 242 - http://rubyforge.org/tracker/index.php?func=detail&aid=17412&group_id=1917&atid=7477
|
79
|
+
* 15977 in revision 198 - http://rubyforge.org/tracker/index.php?func=detail&aid=15977&group_id=1917&atid=7477
|
80
|
+
* 11885 in revision 156 - http://rubyforge.org/tracker/index.php?func=detail&aid=11885&group_id=1917&atid=7477
|
81
|
+
|
82
|
+
= 0.5.5 (r167)
|
83
|
+
|
84
|
+
- Renamed Matches parameter matcher to RegexpMatches for clarity.
|
85
|
+
- Added noframes tag to rdoc index to assist Google.
|
86
|
+
|
87
|
+
= 0.5.4 (r166)
|
88
|
+
|
89
|
+
- Added matches parameter matcher for matching regular expressions.
|
90
|
+
|
91
|
+
= 0.5.3 (r165)
|
92
|
+
|
93
|
+
- Attempt to fix packaging problems by switching to newer version (1.15.1) of gnutar and setting COPY_EXTENDED_ATTRIBUTES_DISABLE environment variable.
|
94
|
+
- Removed unused ExpectationSequenceError exception.
|
95
|
+
- Added instance_of and kind_of parameter matchers.
|
96
|
+
- Added Google Webmaster meta tag to rdoc template header.
|
97
|
+
- Put Google Webmaster meta tag in the right header i.e. the one for the index page.
|
98
|
+
|
99
|
+
= 0.5.2 (r159)
|
100
|
+
|
101
|
+
- Fix bug 11885 - "never doesn't work with stub_everything" submitted by Alexander Lang. In fixing this bug, also fixed undiscoverd bug where expected & actual invocation counts were being incorrectly reported which seems to have been introduced when fixes were added for invocation dispatch (see MockedMethodDispatchAcceptanceTest).
|
102
|
+
- Previously when an expectation did not allow more invocations, it was treated as not matching. Now we prefer matching expectations which allow more invocations, but still match expectations which cannot allow more invocations. I think this may be overcomplicating things, but let's see how it goes.
|
103
|
+
|
104
|
+
= 0.5.1 (r149)
|
105
|
+
|
106
|
+
- Fixed bug #11583 "Mocha 0.5.0 throwing unexpected warnings". Also switched on ruby warning for all rake test tasks. Fixed majority of warnings, but some left to fix.
|
107
|
+
|
108
|
+
= 0.5.0 (r147)
|
109
|
+
|
110
|
+
- Parameter Matchers - I’ve added a few Hamcrest-style parameter matchers which are designed to be used inside Expectation#with. The following matchers are currently available: anything(), includes(), has_key(), has_value(), has_entry(), all_of() & any_of(). More to follow soon. The idea is eventually to get rid of the nasty parameter_block option on Expectation#with.
|
111
|
+
|
112
|
+
object = mock()
|
113
|
+
object.expects(:method).with(has_key('key_1'))
|
114
|
+
object.method('key_1' => 1, 'key_2' => 2)
|
115
|
+
# no verification error raised
|
116
|
+
|
117
|
+
object = mock()
|
118
|
+
object.expects(:method).with(has_key('key_1'))
|
119
|
+
object.method('key_2' => 2)
|
120
|
+
# verification error raised, because method was not called with Hash containing key: 'key_1'
|
121
|
+
|
122
|
+
- Values Returned and Exceptions Raised on Consecutive Invocations - Allow multiple calls to Expectation#returns and Expectation#raises to build up a sequence of responses to invocations on the mock. Added syntactic sugar method Expectation#then to allow more readable expectations.
|
123
|
+
|
124
|
+
object = mock()
|
125
|
+
object.stubs(:method).returns(1, 2).then.raises(Exception).then.returns(4)
|
126
|
+
object.method # => 1
|
127
|
+
object.method # => 2
|
128
|
+
object.method # => raises exception of class Exception
|
129
|
+
object.method # => 4
|
130
|
+
|
131
|
+
- Yields on Consecutive Invocations - Allow multiple calls to yields on single expectation to allow yield parameters to be specified for consecutive invocations.
|
132
|
+
|
133
|
+
object = mock()
|
134
|
+
object.stubs(:method).yields(1, 2).then.yields(3)
|
135
|
+
object.method { |*values| p values } # => [1, 2]
|
136
|
+
object.method { |*values| p values } # => [3]
|
137
|
+
|
138
|
+
- Multiple Yields on Single Invocation - Added Expectation#multiple_yields to allow a mocked or stubbed method to yield multiple times for a single invocation.
|
139
|
+
|
140
|
+
object = mock()
|
141
|
+
object.stubs(:method).multiple_yields([1, 2], [3])
|
142
|
+
object.method { |*values| p values } # => [1, 2] # => [3]
|
143
|
+
|
144
|
+
- Invocation Dispatch - Expectations were already being matched in reverse order i.e. the most recently defined one was being found first. This is still the case, but we now stop matching an expectation when its maximum number of expected invocations is reached. c.f. JMock v1. A stub will never stop matching by default. Hopefully this means we can soon get rid of the need to pass a Proc to Expectation#returns.
|
145
|
+
|
146
|
+
object = mock()
|
147
|
+
object.stubs(:method).returns(2)
|
148
|
+
object.expects(:method).once.returns(1)
|
149
|
+
object.method # => 1
|
150
|
+
object.method # => 2
|
151
|
+
object.method # => 2
|
152
|
+
# no verification error raised
|
153
|
+
|
154
|
+
# The following should still work...
|
155
|
+
|
156
|
+
Time.stubs(:now).returns(Time.parse('Mon Jan 01 00:00:00 UTC 2007'))
|
157
|
+
Time.now # => Mon Jan 01 00:00:00 UTC 2007
|
158
|
+
Time.stubs(:now).returns(Time.parse('Thu Feb 01 00:00:00 UTC 2007'))
|
159
|
+
Time.now # => Thu Feb 01 00:00:00 UTC 2007
|
160
|
+
|
161
|
+
- Deprecate passing an instance of Proc to Expectation#returns.
|
162
|
+
- Explicitly include all Rakefile dependencies in project.
|
163
|
+
- Fixed old Stubba example.
|
164
|
+
- Fix so that it is possible for a stubbed method to raise an Interrupt exception without a message in Ruby 1.8.6
|
165
|
+
- Added responds_like and quacks_like.
|
166
|
+
- Capture standard object methods before Mocha adds any.
|
167
|
+
- Added Expectation#once method to make interface less surprising.
|
168
|
+
- Use Rake::TestTask to run tests. Created three separate tasks to run unit, integration & acceptance tests. Split inspect_test into one file per TestCase. Deleted superfluous all_tests file.
|
169
|
+
- Fiddled with mocha_inspect and tests to give more sensible results on x86 platform.
|
170
|
+
- Fixed bug #7834 "infinite_range.rb makes incorrect assumption about to_f" logged by James Moore.
|
171
|
+
|
172
|
+
= 0.4.0 (r92)
|
173
|
+
|
174
|
+
- Allow naming of mocks (patch from Chris Roos).
|
175
|
+
- Specify multiple return values for consecutive calls.
|
176
|
+
- Improved consistency of expectation error messages.
|
177
|
+
- Allow mocking of Object instance methods e.g. kind_of?, type.
|
178
|
+
- Provide aliased versions of #expects and #stubs to allow mocking of these methods.
|
179
|
+
- Added at_least, at_most, at_most_once methods to expectation.
|
180
|
+
- Allow expects and stubs to take a hash of method and return values.
|
181
|
+
- Eliminate warning: "instance variable @yield not initialized" (patch from Xavier Shay).
|
182
|
+
- Restore instance methods on partial mocks (patch from Chris Roos).
|
183
|
+
- Allow stubbing of a method with non-word characters in its name (patch from Paul Battley).
|
184
|
+
- Removed coupling to Test::Unit.
|
185
|
+
- Allow specified exception instance to be raised (patch from Chris Roos).
|
186
|
+
- Make mock object_id appear in hex like normal Ruby inspect (patch from Paul Battley).
|
187
|
+
- Fix path to object.rb in rdoc rake task (patch from Tomas Pospisek).
|
188
|
+
- Reverse order in which expectations are matched, so that last expectation is matched first. This allows e.g. a call to #stubs to be effectively overridden by a call to #expects (patch from Tobias Lutke).
|
189
|
+
- Stubba & SmartTestCase modules incorporated into Mocha module so only need to require 'mocha' - no longer need to require 'stubba'.
|
190
|
+
- AutoMocha removed.
|
191
|
+
|
192
|
+
= 0.3.3
|
193
|
+
|
194
|
+
- Quick bug fix to restore instance methods on partial mocks (for Kevin Clark).
|
195
|
+
|
196
|
+
= 0.3.2
|
197
|
+
|
198
|
+
- Examples added.
|
199
|
+
|
200
|
+
= 0.3.1
|
201
|
+
|
202
|
+
- Dual licensing with MIT license added.
|
203
|
+
|
204
|
+
= 0.3.0
|
205
|
+
|
206
|
+
* Rails plugin.
|
207
|
+
* Auto-verify for expectations on concrete classes.
|
208
|
+
* Include each expectation verification in the test result assertion count.
|
209
|
+
* Filter out noise from assertion backtraces.
|
210
|
+
* Point assertion backtrace to line where failing expectation was created.
|
211
|
+
* New yields method for expectations.
|
212
|
+
* Create stubs which stub all method calls.
|
213
|
+
* Mocks now respond_to? expected methods.
|
214
|
+
|
215
|
+
= 0.2.1
|
216
|
+
|
217
|
+
* Rename MochaAcceptanceTest::Rover#move method to avoid conflict with Rake (in Ruby 1.8.4 only?)
|
218
|
+
|
219
|
+
= 0.2.0
|
220
|
+
|
221
|
+
* Small change to SetupAndTeardown#teardown_stubs suggested by Luke Redpath (http://www.lukeredpath.co.uk) to allow use of Stubba with RSpec (http://rspec.rubyforge.org).
|
222
|
+
* Reorganized directory structure and extracted addition of setup and teardown methods into SmartTestCase mini-library.
|
223
|
+
* Addition of auto-verify for Mocha (but not Stubba). This means there is more significance in the choice of expects or stubs in that any expects on a mock will automatically get verified.
|
224
|
+
|
225
|
+
So instead of...
|
226
|
+
|
227
|
+
wotsit = Mocha.new
|
228
|
+
wotsit.expects(:thingummy).with(5).returns(10)
|
229
|
+
doobrey = Doobrey.new(wotsit)
|
230
|
+
doobrey.hoojamaflip
|
231
|
+
wotsit.verify
|
232
|
+
|
233
|
+
you need to do...
|
234
|
+
|
235
|
+
wotsit = mock()
|
236
|
+
wotsit.expects(:thingummy).with(5).returns(10)
|
237
|
+
doobrey = Doobrey.new(wotsit)
|
238
|
+
doobrey.hoojamaflip
|
239
|
+
# no need to verify
|
240
|
+
|
241
|
+
There are also shortcuts as follows...
|
242
|
+
|
243
|
+
instead of...
|
244
|
+
|
245
|
+
wotsit = Mocha.new
|
246
|
+
wotsit.expects(:thingummy).returns(10)
|
247
|
+
wotsit.expects(:summat).returns(25)
|
248
|
+
|
249
|
+
you can have...
|
250
|
+
|
251
|
+
wotsit = mock(:thingummy => 5, :summat => 25)
|
252
|
+
|
253
|
+
and instead of...
|
254
|
+
|
255
|
+
wotsit = Mocha.new
|
256
|
+
wotsit.stubs(:thingummy).returns(10)
|
257
|
+
wotsit.stubs(:summat).returns(25)
|
258
|
+
|
259
|
+
you can have...
|
260
|
+
|
261
|
+
wotsit = stub(:thingummy => 5, :summat => 25)
|
262
|
+
|
263
|
+
= 0.1.2
|
264
|
+
|
265
|
+
* Minor tweaks
|
266
|
+
|
267
|
+
= 0.1.1
|
268
|
+
|
269
|
+
* Initial release.
|
data/Rakefile
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
require 'rake/rdoctask'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
module Mocha
|
6
|
+
VERSION = "0.9.5"
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run all tests"
|
10
|
+
task 'default' => ['test:units', 'test:acceptance', 'test:performance']
|
11
|
+
|
12
|
+
namespace 'test' do
|
13
|
+
|
14
|
+
unit_tests = FileList['test/unit/**/*_test.rb']
|
15
|
+
acceptance_tests = FileList['test/acceptance/*_test.rb']
|
16
|
+
|
17
|
+
desc "Run unit tests"
|
18
|
+
Rake::TestTask.new('units') do |t|
|
19
|
+
t.libs << 'test'
|
20
|
+
t.test_files = unit_tests
|
21
|
+
t.verbose = true
|
22
|
+
t.warning = true
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Run acceptance tests"
|
26
|
+
Rake::TestTask.new('acceptance') do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.test_files = acceptance_tests
|
29
|
+
t.verbose = true
|
30
|
+
t.warning = true
|
31
|
+
end
|
32
|
+
|
33
|
+
# require 'rcov/rcovtask'
|
34
|
+
# Rcov::RcovTask.new('coverage') do |t|
|
35
|
+
# t.libs << 'test'
|
36
|
+
# t.test_files = unit_tests + acceptance_tests
|
37
|
+
# t.verbose = true
|
38
|
+
# t.warning = true
|
39
|
+
# t.rcov_opts << '--sort coverage'
|
40
|
+
# t.rcov_opts << '--xref'
|
41
|
+
# end
|
42
|
+
|
43
|
+
desc "Run performance tests"
|
44
|
+
task 'performance' do
|
45
|
+
require 'test/acceptance/stubba_example_test'
|
46
|
+
require 'test/acceptance/mocha_example_test'
|
47
|
+
iterations = 1000
|
48
|
+
puts "\nBenchmarking with #{iterations} iterations..."
|
49
|
+
[MochaExampleTest, StubbaExampleTest].each do |test_case|
|
50
|
+
puts "#{test_case}: #{benchmark_test_case(test_case, iterations)} seconds."
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
def benchmark_test_case(klass, iterations)
|
57
|
+
require 'benchmark'
|
58
|
+
require 'test/unit/ui/console/testrunner'
|
59
|
+
begin
|
60
|
+
require 'test/unit/ui/console/outputlevel'
|
61
|
+
silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT }
|
62
|
+
rescue LoadError
|
63
|
+
silent_option = Test::Unit::UI::SILENT
|
64
|
+
end
|
65
|
+
time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, silent_option) } }
|
66
|
+
end
|
67
|
+
|
68
|
+
desc 'Generate RDoc'
|
69
|
+
Rake::RDocTask.new('rdoc') do |task|
|
70
|
+
task.main = 'README'
|
71
|
+
task.title = "Mocha #{Mocha::VERSION}"
|
72
|
+
task.rdoc_dir = 'doc'
|
73
|
+
task.template = File.expand_path(File.join(File.dirname(__FILE__), "templates", "html_with_google_analytics"))
|
74
|
+
task.rdoc_files.include(
|
75
|
+
'README',
|
76
|
+
'RELEASE',
|
77
|
+
'COPYING',
|
78
|
+
'MIT-LICENSE',
|
79
|
+
'agiledox.txt',
|
80
|
+
'lib/mocha/api.rb',
|
81
|
+
'lib/mocha/mock.rb',
|
82
|
+
'lib/mocha/expectation.rb',
|
83
|
+
'lib/mocha/object.rb',
|
84
|
+
'lib/mocha/parameter_matchers.rb',
|
85
|
+
'lib/mocha/parameter_matchers',
|
86
|
+
'lib/mocha/state_machine.rb',
|
87
|
+
'lib/mocha/configuration.rb',
|
88
|
+
'lib/mocha/stubbing_error.rb'
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Generate all documentation"
|
93
|
+
task 'generate_docs' => ['clobber_rdoc', 'rdoc', 'examples', 'agiledox.txt']
|
94
|
+
|
95
|
+
desc "Upload RDoc to RubyForge"
|
96
|
+
task 'publish_docs' do
|
97
|
+
require 'rake/contrib/sshpublisher'
|
98
|
+
Rake::SshDirPublisher.new("jamesmead@rubyforge.org", "/var/www/gforge-projects/mocha", "doc").upload
|
99
|
+
end
|
100
|
+
|
101
|
+
desc "Generate agiledox-like documentation for tests"
|
102
|
+
file 'agiledox.txt' do
|
103
|
+
File.open('agiledox.txt', 'w') do |output|
|
104
|
+
tests = FileList['test/**/*_test.rb']
|
105
|
+
tests.each do |file|
|
106
|
+
m = %r".*/([^/].*)_test.rb".match(file)
|
107
|
+
output << m[1]+" should:\n"
|
108
|
+
test_definitions = File::readlines(file).select {|line| line =~ /.*def test.*/}
|
109
|
+
test_definitions.sort.each do |definition|
|
110
|
+
m = %r"test_(should_)?(.*)".match(definition)
|
111
|
+
output << " - "+m[2].gsub(/_/," ") << "\n"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
desc "Convert example ruby files to syntax-highlighted html"
|
118
|
+
task 'examples' do
|
119
|
+
require 'coderay'
|
120
|
+
mkdir_p 'doc/examples'
|
121
|
+
File.open('doc/examples/coderay.css', 'w') do |output|
|
122
|
+
output << CodeRay::Encoders[:html]::CSS.new.stylesheet
|
123
|
+
end
|
124
|
+
['mocha', 'stubba', 'misc'].each do |filename|
|
125
|
+
File.open("doc/examples/#{filename}.html", 'w') do |file|
|
126
|
+
file << "<html>"
|
127
|
+
file << "<head>"
|
128
|
+
file << %q(<link rel="stylesheet" media="screen" href="coderay.css" type="text/css">)
|
129
|
+
file << "</head>"
|
130
|
+
file << "<body>"
|
131
|
+
file << CodeRay.scan_file("examples/#{filename}.rb").html.div
|
132
|
+
file << "</body>"
|
133
|
+
file << "</html>"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
Gem.manage_gems if Gem::RubyGemsVersion < '1.2.0'
|
139
|
+
|
140
|
+
def build_specification(version = Mocha::VERSION)
|
141
|
+
Gem::Specification.new do |s|
|
142
|
+
s.name = "mocha"
|
143
|
+
s.summary = "Mocking and stubbing library"
|
144
|
+
s.version = "#{version}.0.#{Time.now.to_i}"
|
145
|
+
s.platform = Gem::Platform::RUBY
|
146
|
+
s.author = 'James Mead'
|
147
|
+
s.description = <<-EOF
|
148
|
+
Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
|
149
|
+
EOF
|
150
|
+
s.email = 'mocha-developer@googlegroups.com'
|
151
|
+
s.homepage = 'http://mocha.rubyforge.org'
|
152
|
+
s.rubyforge_project = 'mocha'
|
153
|
+
|
154
|
+
s.has_rdoc = true
|
155
|
+
s.extra_rdoc_files = ['README', 'COPYING']
|
156
|
+
s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
|
157
|
+
|
158
|
+
s.add_dependency('rake')
|
159
|
+
s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
specification = build_specification
|
164
|
+
|
165
|
+
Rake::GemPackageTask.new(specification) do |package|
|
166
|
+
package.need_zip = true
|
167
|
+
package.need_tar = true
|
168
|
+
end
|
169
|
+
|
170
|
+
desc 'Generate updated gemspec with unique version, which will cause gem to be auto-built on github.'
|
171
|
+
task :update_gemspec do
|
172
|
+
File.open('mocha.gemspec', 'w') do |output|
|
173
|
+
output << build_specification(Mocha::VERSION + '.' + Time.now.strftime('%Y%m%d%H%M%S')).to_ruby
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
task 'verify_user' do
|
178
|
+
raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
|
179
|
+
end
|
180
|
+
|
181
|
+
task 'verify_password' do
|
182
|
+
raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
|
183
|
+
end
|
184
|
+
|
185
|
+
desc "Publish package files on RubyForge."
|
186
|
+
task 'publish_packages' => ['verify_user', 'verify_password', 'clobber_package', 'package'] do
|
187
|
+
require 'meta_project'
|
188
|
+
require 'rake/contrib/xforge'
|
189
|
+
release_files = FileList[
|
190
|
+
"pkg/mocha-#{Mocha::VERSION}.gem",
|
191
|
+
"pkg/mocha-#{Mocha::VERSION}.tgz",
|
192
|
+
"pkg/mocha-#{Mocha::VERSION}.zip"
|
193
|
+
]
|
194
|
+
|
195
|
+
Rake::XForge::Release.new(MetaProject::Project::XForge::RubyForge.new('mocha')) do |release|
|
196
|
+
release.user_name = ENV['RUBYFORGE_USER']
|
197
|
+
release.password = ENV['RUBYFORGE_PASSWORD']
|
198
|
+
release.files = release_files.to_a
|
199
|
+
release.release_name = "Mocha #{Mocha::VERSION}"
|
200
|
+
release.release_changes = ''
|
201
|
+
release.release_notes = ''
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
desc "Do a full release."
|
206
|
+
task 'release' => ['default', 'generate_docs', 'publish_packages', 'publish_docs', 'update_gemspec'] do
|
207
|
+
puts
|
208
|
+
puts "*** Remember to commit newly generated gemspec after release ***"
|
209
|
+
puts
|
210
|
+
end
|
211
|
+
|
212
|
+
desc "Generate a gemspec file"
|
213
|
+
task :gemspec do
|
214
|
+
File.open("#{specification.name}.gemspec", 'w') do |f|
|
215
|
+
f.write specification.to_yaml
|
216
|
+
end
|
217
|
+
end
|
data/examples/misc.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class MiscExampleTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_mocking_a_class_method
|
7
|
+
product = Product.new
|
8
|
+
Product.expects(:find).with(1).returns(product)
|
9
|
+
assert_equal product, Product.find(1)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_mocking_an_instance_method_on_a_real_object
|
13
|
+
product = Product.new
|
14
|
+
product.expects(:save).returns(true)
|
15
|
+
assert product.save
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_stubbing_instance_methods_on_real_objects
|
19
|
+
prices = [stub(:pence => 1000), stub(:pence => 2000)]
|
20
|
+
product = Product.new
|
21
|
+
product.stubs(:prices).returns(prices)
|
22
|
+
assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_stubbing_an_instance_method_on_all_instances_of_a_class
|
26
|
+
Product.any_instance.stubs(:name).returns('stubbed_name')
|
27
|
+
product = Product.new
|
28
|
+
assert_equal 'stubbed_name', product.name
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_traditional_mocking
|
32
|
+
object = mock()
|
33
|
+
object.expects(:expected_method).with(:p1, :p2).returns(:result)
|
34
|
+
assert_equal :result, object.expected_method(:p1, :p2)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_shortcuts
|
38
|
+
object = stub(:method1 => :result1, :method2 => :result2)
|
39
|
+
assert_equal :result1, object.method1
|
40
|
+
assert_equal :result2, object.method2
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/examples/mocha.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class Enterprise
|
2
|
+
|
3
|
+
def initialize(dilithium)
|
4
|
+
@dilithium = dilithium
|
5
|
+
end
|
6
|
+
|
7
|
+
def go(warp_factor)
|
8
|
+
warp_factor.times { @dilithium.nuke(:anti_matter) }
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'test/unit'
|
14
|
+
require 'mocha'
|
15
|
+
|
16
|
+
class EnterpriseTest < Test::Unit::TestCase
|
17
|
+
|
18
|
+
def test_should_boldly_go
|
19
|
+
dilithium = mock()
|
20
|
+
dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
|
21
|
+
enterprise = Enterprise.new(dilithium)
|
22
|
+
enterprise.go(2)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/examples/stubba.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
class Order
|
2
|
+
|
3
|
+
attr_accessor :shipped_on
|
4
|
+
|
5
|
+
def total_cost
|
6
|
+
line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
|
7
|
+
end
|
8
|
+
|
9
|
+
def total_weight
|
10
|
+
line_items.inject(0) { |total, line_item| total + line_item.weight }
|
11
|
+
end
|
12
|
+
|
13
|
+
def shipping_cost
|
14
|
+
total_weight * 5 + 10
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
|
19
|
+
def find_all
|
20
|
+
# Database.connection.execute('select * from orders...
|
21
|
+
end
|
22
|
+
|
23
|
+
def number_shipped_since(date)
|
24
|
+
find_all.select { |order| order.shipped_on > date }.length
|
25
|
+
end
|
26
|
+
|
27
|
+
def unshipped_value
|
28
|
+
find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'test/unit'
|
36
|
+
require 'mocha'
|
37
|
+
|
38
|
+
class OrderTest < Test::Unit::TestCase
|
39
|
+
|
40
|
+
# illustrates stubbing instance method
|
41
|
+
def test_should_calculate_shipping_cost_based_on_total_weight
|
42
|
+
order = Order.new
|
43
|
+
order.stubs(:total_weight).returns(10)
|
44
|
+
assert_equal 60, order.shipping_cost
|
45
|
+
end
|
46
|
+
|
47
|
+
# illustrates stubbing class method
|
48
|
+
def test_should_count_number_of_orders_shipped_after_specified_date
|
49
|
+
now = Time.now; week_in_secs = 7 * 24 * 60 * 60
|
50
|
+
order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
|
51
|
+
order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
|
52
|
+
Order.stubs(:find_all).returns([order_1, order_2])
|
53
|
+
assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
|
54
|
+
end
|
55
|
+
|
56
|
+
# illustrates stubbing instance method for all instances of a class
|
57
|
+
def test_should_calculate_value_of_unshipped_orders
|
58
|
+
Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
|
59
|
+
Order.any_instance.stubs(:shipped_on).returns(nil)
|
60
|
+
Order.any_instance.stubs(:total_cost).returns(10)
|
61
|
+
assert_equal 30, Order.unshipped_value
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|