flexmock 1.3.3 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.autotest +3 -0
  3. data/.gitignore +14 -0
  4. data/.togglerc +7 -0
  5. data/.travis.yml +5 -0
  6. data/.yardopts +2 -0
  7. data/CHANGES +11 -0
  8. data/Gemfile +1 -4
  9. data/README.md +39 -11
  10. data/Rakefile +6 -217
  11. data/doc/examples/rspec_examples_spec.rb +244 -0
  12. data/doc/examples/test_unit_examples_test.rb +240 -0
  13. data/doc/jamis.rb +591 -0
  14. data/flexmock.gemspec +33 -0
  15. data/lib/flexmock.rb +0 -1
  16. data/lib/flexmock/composite_expectation.rb +1 -1
  17. data/lib/flexmock/core.rb +3 -7
  18. data/lib/flexmock/core_class_methods.rb +5 -1
  19. data/lib/flexmock/default_framework_adapter.rb +2 -2
  20. data/lib/flexmock/expectation.rb +29 -3
  21. data/lib/flexmock/expectation_director.rb +1 -1
  22. data/lib/flexmock/minitest.rb +13 -0
  23. data/lib/flexmock/minitest_extensions.rb +26 -0
  24. data/lib/flexmock/minitest_integration.rb +111 -0
  25. data/lib/flexmock/mock_container.rb +1 -2
  26. data/lib/flexmock/partial_mock.rb +61 -104
  27. data/lib/flexmock/recorder.rb +1 -2
  28. data/lib/flexmock/rspec.rb +6 -3
  29. data/lib/flexmock/test_unit_integration.rb +14 -0
  30. data/lib/flexmock/validators.rb +5 -4
  31. data/lib/flexmock/version.rb +1 -9
  32. data/rakelib/metrics.rake +40 -0
  33. data/rakelib/preview.rake +4 -0
  34. data/rakelib/tags.rake +18 -0
  35. data/todo.txt +20 -0
  36. metadata +61 -86
  37. data/Gemfile.lock +0 -20
  38. data/doc/examples/rspec_examples_spec.rdoc +0 -245
  39. data/doc/examples/test_unit_examples_test.rdoc +0 -241
  40. data/test/aliasing_test.rb +0 -66
  41. data/test/assert_spy_called_test.rb +0 -119
  42. data/test/base_class_test.rb +0 -71
  43. data/test/based_partials_test.rb +0 -51
  44. data/test/container_methods_test.rb +0 -118
  45. data/test/default_framework_adapter_test.rb +0 -38
  46. data/test/demeter_mocking_test.rb +0 -191
  47. data/test/deprecated_methods_test.rb +0 -225
  48. data/test/examples_from_readme_test.rb +0 -157
  49. data/test/expectation_description_test.rb +0 -80
  50. data/test/extended_should_receive_test.rb +0 -69
  51. data/test/flexmodel_test.rb +0 -54
  52. data/test/mock_builder_test.rb +0 -68
  53. data/test/naming_test.rb +0 -84
  54. data/test/new_instances_test.rb +0 -215
  55. data/test/object_extensions_test.rb +0 -25
  56. data/test/partial_mock_test.rb +0 -458
  57. data/test/record_mode_test.rb +0 -158
  58. data/test/redirect_error.rb +0 -16
  59. data/test/rspec_integration/integration_spec.rb +0 -56
  60. data/test/rspec_integration/spy_example_spec.rb +0 -207
  61. data/test/samples_test.rb +0 -283
  62. data/test/should_ignore_missing_test.rb +0 -84
  63. data/test/should_receive_test.rb +0 -1155
  64. data/test/spys_test.rb +0 -215
  65. data/test/symbol_extensions_test.rb +0 -8
  66. data/test/test_class_extensions.rb +0 -34
  67. data/test/test_setup.rb +0 -92
  68. data/test/test_unit_integration/auto_test_unit_test.rb +0 -42
  69. data/test/test_unit_integration/minitest_teardown_test.rb +0 -14
  70. data/test/tu_integration_test.rb +0 -99
  71. data/test/undefined_test.rb +0 -87
@@ -56,9 +56,8 @@ class FlexMock
56
56
  # Record an expectation for receiving the method +sym+ with the
57
57
  # given arguments.
58
58
  def method_missing(sym, *args, &block)
59
- location = caller.first
60
59
  expectation = @mock.
61
- flexmock_define_expectation(location, sym).
60
+ flexmock_define_expectation(caller, sym).
62
61
  and_return(&block)
63
62
  if strict?
64
63
  args = args.collect { |arg| eq(arg) }
@@ -26,15 +26,18 @@ class FlexMock
26
26
  SpecModule::Expectations.fail_with(msg) unless yield
27
27
  end
28
28
 
29
- def assert_equal(a, b, msg=nil)
30
- message = msg || "Expected equal"
31
- make_assertion(message + "\n<#{a}> expected, but was\n<#{b}>") { a == b }
29
+ def check(msg, &block)
30
+ check(msg, &block)
32
31
  end
33
32
 
34
33
  class AssertionFailedError < StandardError; end
34
+
35
35
  def assertion_failed_error
36
36
  SpecModule::Expectations::ExpectationNotMetError
37
37
  end
38
+ def check_failed_error
39
+ assertion_failed_error
40
+ end
38
41
  end
39
42
 
40
43
  @framework_adapter = RSpecFrameworkAdapter.new
@@ -45,6 +45,12 @@ class FlexMock
45
45
  class TestUnitFrameworkAdapter
46
46
  include Test::Unit::Assertions
47
47
 
48
+ attr_accessor :assertions
49
+
50
+ def initialize
51
+ @assertions = 0
52
+ end
53
+
48
54
  def make_assertion(msg, &block)
49
55
  unless yield
50
56
  msg = msg.call if msg.is_a?(Proc)
@@ -55,11 +61,19 @@ class FlexMock
55
61
  raise ex
56
62
  end
57
63
 
64
+ def check(msg, &block)
65
+ make_assertion(msg, &block)
66
+ end
67
+
58
68
  def assertion_failed_error
59
69
  defined?(Test::Unit::AssertionFailedError) ?
60
70
  Test::Unit::AssertionFailedError :
61
71
  MiniTest::Assertion
62
72
  end
73
+
74
+ def check_failed_error
75
+ assertion_failed_error
76
+ end
63
77
  end
64
78
 
65
79
  @framework_adapter = TestUnitFrameworkAdapter.new
@@ -50,11 +50,12 @@ class FlexMock
50
50
  @limit.to_s
51
51
  end
52
52
 
53
+ class ValidationFailed < RuntimeError
54
+ end
55
+
53
56
  def validate_count(n, &block)
54
- @exp.flexmock_location_filter do
55
- FlexMock.framework_adapter.make_assertion(
56
- lambda { construct_validation_count_error_message(n) },
57
- &block)
57
+ unless yield
58
+ raise ValidationFailed, construct_validation_count_error_message(n)
58
59
  end
59
60
  end
60
61
 
@@ -1,11 +1,3 @@
1
1
  class FlexMock
2
- module Version
3
- NUMBERS = [
4
- MAJOR = 1,
5
- MINOR = 3,
6
- BUILD = 3,
7
- ]
8
- end
9
-
10
- VERSION = Version::NUMBERS.join('.')
2
+ VERSION = "2.0.0.rc1"
11
3
  end
@@ -0,0 +1,40 @@
1
+ METRICS_FILES = FileList['lib/**/*.rb']
2
+
3
+ task :metrics => [:cane, :flog, :flay]
4
+
5
+ task :flog, [:all] do |t, args|
6
+ flags = args.all ? "--all" : ""
7
+ flags = "-m #{flags}"
8
+ Bundler.with_clean_env do
9
+ puts "\nFLOG:"
10
+ sh "flog #{flags} #{METRICS_FILES}" do |status, flag|
11
+ if status.nil?
12
+ puts "Install flog with: 'gem install flog'"
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ task :flay do
19
+ Bundler.with_clean_env do
20
+ puts "\nFLAY:"
21
+ sh "flay #{METRICS_FILES}" do |status, flag|
22
+ if status.nil?
23
+ puts "Install flay with: 'gem install flay'"
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ task :cane, [:max_line] do |t, args|
30
+ max_line = args.max_line || 90
31
+ Bundler.with_clean_env do
32
+ puts "\nCANE:"
33
+ sh "cane --style-measure #{max_line} --no-doc" do |status, flag|
34
+ if status.nil?
35
+ puts "DBG: [status, flag]=#{[status, flag].inspect}"
36
+ puts "Install cane with: 'gem install cane'"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+
2
+ task :preview do
3
+ sh "ghpreview #{FileList['README.{md,markdown}']}"
4
+ end
data/rakelib/tags.rake ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- ruby -*-
3
+
4
+ module Tags
5
+ RUBY_FILES = FileList['**/*.rb'].exclude("pkg")
6
+ PROG = ENV['TAGS'] || 'ctags'
7
+ end
8
+
9
+ namespace "tags" do
10
+ desc "Update the Tags file for emacs"
11
+ task :emacs => Tags::RUBY_FILES do
12
+ puts "Making Emacs TAGS file"
13
+ sh "#{Tags::PROG} -e #{Tags::RUBY_FILES}", :verbose => false
14
+ end
15
+ end
16
+
17
+ desc "Update the Tags file for emacs"
18
+ task :tags => ["tags:emacs"]
data/todo.txt ADDED
@@ -0,0 +1,20 @@
1
+ Short term things to do:
2
+
3
+ * Explicit => Explicitly
4
+
5
+ DONE
6
+
7
+ * Consolidate mock argument matching and spy argument matching
8
+ * Consolidate mock block detection and spy block detection.
9
+ * Consolidate the :on option with mocks
10
+ * Pass through mode for partial mocks
11
+ * Update readme
12
+ * auto-mock class when using base_class
13
+
14
+ IDEAS
15
+
16
+ spy.should have_received(:foo).with(arg1, arg2) # PREFERRED
17
+ spy.should have_received.foo(arg1, arg2)
18
+
19
+ assert_called spy, :foo, arg1, arg2
20
+ assert_spy(spy).received(:foo).with(arg1, arg2)
metadata CHANGED
@@ -1,63 +1,66 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 2.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Weirich
8
+ - Sylvain Joyeux
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-01-20 00:00:00.000000000 Z
12
- dependencies: []
13
- description: "\n FlexMock is a extremely simple mock object class compatible\n
14
- \ with the Test::Unit framework. Although the FlexMock's\n interface is
15
- simple, it is very flexible.\n "
16
- email: jim.weirich@gmail.com
12
+ date: 2015-09-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: "\n FlexMock is a extremely simple mock object class compatible\n
43
+ \ with the Minitest framework. Although the FlexMock's\n interface is simple,
44
+ it is very flexible.\n "
45
+ email: sylvain.joyeux@m4x.org
17
46
  executables: []
18
47
  extensions: []
19
- extra_rdoc_files:
20
- - doc/index.rdoc
21
- - CHANGES
22
- - doc/GoogleExample.rdoc
23
- - doc/releases/flexmock-0.4.0.rdoc
24
- - doc/releases/flexmock-0.4.1.rdoc
25
- - doc/releases/flexmock-0.4.2.rdoc
26
- - doc/releases/flexmock-0.4.3.rdoc
27
- - doc/releases/flexmock-0.5.0.rdoc
28
- - doc/releases/flexmock-0.5.1.rdoc
29
- - doc/releases/flexmock-0.6.0.rdoc
30
- - doc/releases/flexmock-0.6.1.rdoc
31
- - doc/releases/flexmock-0.6.2.rdoc
32
- - doc/releases/flexmock-0.6.3.rdoc
33
- - doc/releases/flexmock-0.6.4.rdoc
34
- - doc/releases/flexmock-0.7.0.rdoc
35
- - doc/releases/flexmock-0.7.1.rdoc
36
- - doc/releases/flexmock-0.8.0.rdoc
37
- - doc/releases/flexmock-0.8.2.rdoc
38
- - doc/releases/flexmock-0.8.3.rdoc
39
- - doc/releases/flexmock-0.8.4.rdoc
40
- - doc/releases/flexmock-0.8.5.rdoc
41
- - doc/releases/flexmock-0.9.0.rdoc
42
- - doc/releases/flexmock-1.0.0.rdoc
43
- - doc/releases/flexmock-1.0.3.rdoc
44
- - doc/releases/flexmock-1.0.4.rdoc
45
- - doc/releases/flexmock-1.1.0.rdoc
46
- - doc/releases/flexmock-1.2.0.rdoc
47
- - doc/releases/flexmock-1.3.0.rdoc
48
- - doc/releases/flexmock-1.3.1.rdoc
49
- - doc/examples/rspec_examples_spec.rdoc
50
- - doc/examples/test_unit_examples_test.rdoc
48
+ extra_rdoc_files: []
51
49
  files:
50
+ - ".autotest"
51
+ - ".gitignore"
52
+ - ".togglerc"
53
+ - ".travis.yml"
54
+ - ".yardopts"
52
55
  - CHANGES
53
56
  - Gemfile
54
- - Gemfile.lock
55
57
  - README.md
56
58
  - Rakefile
57
59
  - doc/GoogleExample.rdoc
58
- - doc/examples/rspec_examples_spec.rdoc
59
- - doc/examples/test_unit_examples_test.rdoc
60
+ - doc/examples/rspec_examples_spec.rb
61
+ - doc/examples/test_unit_examples_test.rb
60
62
  - doc/index.rdoc
63
+ - doc/jamis.rb
61
64
  - doc/releases/flexmock-0.4.0.rdoc
62
65
  - doc/releases/flexmock-0.4.1.rdoc
63
66
  - doc/releases/flexmock-0.4.2.rdoc
@@ -85,6 +88,7 @@ files:
85
88
  - doc/releases/flexmock-1.3.0.rdoc
86
89
  - doc/releases/flexmock-1.3.1.rdoc
87
90
  - flexmock.blurb
91
+ - flexmock.gemspec
88
92
  - install.rb
89
93
  - lib/flexmock.rb
90
94
  - lib/flexmock/argument_matchers.rb
@@ -106,6 +110,9 @@ files:
106
110
  - lib/flexmock/expectation_recorder.rb
107
111
  - lib/flexmock/explicit_needed.rb
108
112
  - lib/flexmock/extensions/active_record_model.rb
113
+ - lib/flexmock/minitest.rb
114
+ - lib/flexmock/minitest_extensions.rb
115
+ - lib/flexmock/minitest_integration.rb
109
116
  - lib/flexmock/mock_builder.rb
110
117
  - lib/flexmock/mock_container.rb
111
118
  - lib/flexmock/noop.rb
@@ -126,63 +133,31 @@ files:
126
133
  - lib/flexmock/undefined.rb
127
134
  - lib/flexmock/validators.rb
128
135
  - lib/flexmock/version.rb
129
- - test/aliasing_test.rb
130
- - test/assert_spy_called_test.rb
131
- - test/base_class_test.rb
132
- - test/based_partials_test.rb
133
- - test/container_methods_test.rb
134
- - test/default_framework_adapter_test.rb
135
- - test/demeter_mocking_test.rb
136
- - test/deprecated_methods_test.rb
137
- - test/examples_from_readme_test.rb
138
- - test/expectation_description_test.rb
139
- - test/extended_should_receive_test.rb
140
- - test/flexmodel_test.rb
141
- - test/mock_builder_test.rb
142
- - test/naming_test.rb
143
- - test/new_instances_test.rb
144
- - test/object_extensions_test.rb
145
- - test/partial_mock_test.rb
146
- - test/record_mode_test.rb
147
- - test/redirect_error.rb
148
- - test/rspec_integration/integration_spec.rb
149
- - test/rspec_integration/spy_example_spec.rb
150
- - test/samples_test.rb
151
- - test/should_ignore_missing_test.rb
152
- - test/should_receive_test.rb
153
- - test/spys_test.rb
154
- - test/symbol_extensions_test.rb
155
- - test/test_class_extensions.rb
156
- - test/test_setup.rb
157
- - test/test_unit_integration/auto_test_unit_test.rb
158
- - test/test_unit_integration/minitest_teardown_test.rb
159
- - test/tu_integration_test.rb
160
- - test/undefined_test.rb
161
- homepage: https://github.com/jimweirich/flexmock
162
- licenses: []
136
+ - rakelib/metrics.rake
137
+ - rakelib/preview.rake
138
+ - rakelib/tags.rake
139
+ - todo.txt
140
+ homepage: https://github.com/doudou/flexmock
141
+ licenses:
142
+ - MIT
163
143
  metadata: {}
164
144
  post_install_message:
165
- rdoc_options:
166
- - "--title"
167
- - FlexMock
168
- - "--main"
169
- - README.rdoc
170
- - "--line-numbers"
145
+ rdoc_options: []
171
146
  require_paths:
172
147
  - lib
173
148
  required_ruby_version: !ruby/object:Gem::Requirement
174
149
  requirements:
175
150
  - - ">="
176
151
  - !ruby/object:Gem::Version
177
- version: '0'
152
+ version: '2.0'
178
153
  required_rubygems_version: !ruby/object:Gem::Requirement
179
154
  requirements:
180
- - - ">="
155
+ - - ">"
181
156
  - !ruby/object:Gem::Version
182
- version: '0'
157
+ version: 1.3.1
183
158
  requirements: []
184
159
  rubyforge_project:
185
- rubygems_version: 2.2.0
160
+ rubygems_version: 2.2.3
186
161
  signing_key:
187
162
  specification_version: 4
188
163
  summary: Simple and Flexible Mock Objects for Testing
data/Gemfile.lock DELETED
@@ -1,20 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- diff-lcs (1.2.5)
5
- rake (10.1.0)
6
- rspec (2.14.1)
7
- rspec-core (~> 2.14.0)
8
- rspec-expectations (~> 2.14.0)
9
- rspec-mocks (~> 2.14.0)
10
- rspec-core (2.14.7)
11
- rspec-expectations (2.14.4)
12
- diff-lcs (>= 1.1.3, < 2.0)
13
- rspec-mocks (2.14.4)
14
-
15
- PLATFORMS
16
- ruby
17
-
18
- DEPENDENCIES
19
- rake (>= 0.9.2.2)
20
- rspec (>= 2.0)
@@ -1,245 +0,0 @@
1
- = FlexMock Examples
2
- RSpec.configure do |config|
3
- config.mock_with :flexmock
4
- end
5
-
6
- describe "Simple Spec" do
7
-
8
- # Simple stubbing of some methods
9
-
10
- it "stubs a couple of methods" do
11
- m = flexmock(:pi => 3.1416, :e => 2.71)
12
- m.pi.should == 3.1416
13
- m.e.should == 2.71
14
- end
15
- end
16
-
17
-
18
- describe "Returning Undefined" do
19
-
20
- # Create a mock object that returns an undefined object for method calls
21
-
22
- it "returns undefined values" do
23
- m = flexmock("mock")
24
- m.should_receive(:divide_by).with(0).
25
- and_return_undefined
26
-
27
- m.divide_by(0).should == FlexMock.undefined
28
- end
29
- end
30
-
31
- describe "Multiple Queries and Single Updates" do
32
-
33
- # Expect multiple queries and a single update
34
-
35
- # Multiple calls to the query method will be allows, and calls may
36
- # have any argument list. Each call to query will return the three
37
- # element array [1, 2, 3]. The call to update must have a specific
38
- # argument of 5.
39
-
40
- it "queries the db" do
41
- db = flexmock('db')
42
- db.should_receive(:query).and_return([1,2,3])
43
- db.should_receive(:update).with(5).and_return(nil).once
44
-
45
- # Test Code
46
-
47
- db.query
48
- db.update(5)
49
- end
50
- end
51
-
52
- describe "Ordered Mocks" do
53
-
54
- # Expect all queries before any updates
55
-
56
- # All the query message must occur before any of the update
57
- # messages.
58
-
59
- it "queries and updates the database" do
60
- db = flexmock('db')
61
- db.should_receive(:query).and_return([1,2,3]).ordered
62
- db.should_receive(:update).and_return(nil).ordered
63
-
64
- # test code here
65
-
66
- db.query
67
- db.update
68
- end
69
- end
70
-
71
- describe "Ordered Mocks" do
72
-
73
- # Expect several queries with different parameters
74
-
75
- # The queries should happen after startup but before finish. The
76
- # queries themselves may happen in any order (because they are in
77
- # the same order group). The first two queries should happen exactly
78
- # once, but the third query (which matches any query call with a
79
- # four character parameter) may be called multiple times (but at
80
- # least once). Startup and finish must also happen exactly once.
81
-
82
- # Also note that we use the <code>with</code> method to match
83
- # different argument values to figure out what value to return.
84
-
85
- it "queries the database in a particular order" do
86
- db = flexmock('db')
87
- db.should_receive(:startup).once.ordered
88
- db.should_receive(:query).with("CPWR").and_return(12.3).
89
- once.ordered(:queries)
90
- db.should_receive(:query).with("MSFT").and_return(10.0).
91
- once.ordered(:queries)
92
- db.should_receive(:query).with(/^....$/).and_return(3.3).
93
- at_least.once.ordered(:queries)
94
- db.should_receive(:finish).once.ordered
95
-
96
- # Test Code
97
-
98
- db.startup
99
- db.query("MSFT")
100
- db.query("XYZY")
101
- db.query("CPWR")
102
- db.finish
103
- end
104
- end
105
-
106
- describe "Ordered Mocks" do
107
-
108
- # Same as above, but using the Record Mode interface
109
-
110
- # The record mode interface offers much the same features as the
111
- # <code>should_receive</code> interface introduced so far, but it
112
- # allows the messages to be sent directly to a recording object
113
- # rather than be specified indirectly using a symbol.
114
-
115
-
116
- it "records the queries for replay" do
117
- db = flexmock('db')
118
- db.should_expect do |rec|
119
- rec.startup.once.ordered
120
- rec.query("CPWR") { 12.3 }.once.ordered(:queries)
121
- rec.query("MSFT") { 10.0 }.once.ordered(:queries)
122
- rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
123
- rec.finish.once.ordered
124
- end
125
-
126
- # Test Code
127
-
128
- db.startup
129
- db.query("MSFT")
130
- db.query("XYZY")
131
- db.query("CPWR")
132
- db.finish
133
- end
134
- end
135
-
136
- describe "Record Mode" do
137
-
138
- # Using Record Mode to record a known, good algorithm for testing
139
-
140
- # Record mode is nice when you have a known, good algorithm that can
141
- # use a recording mock object to record the steps. Then you compare
142
- # the execution of a new algorithm to behavior of the old using the
143
- # recorded expectations in the mock. For this you probably want to
144
- # put the recorder in _strict_ mode so that the recorded
145
- # expectations use exact matching on argument lists, and strict
146
- # ordering of the method calls.
147
-
148
- # <b>Note:</b> This is most useful when there are no queries on the
149
- # mock objects, because the query responses cannot be programmed
150
- # into the recorder object.
151
-
152
- it "compares a know algorithm with a new algorithm" do
153
- builder = flexmock('builder')
154
- builder.should_expect do |rec|
155
- rec.should_be_strict
156
- known_good_way_to_build_xml(rec) # record the messages
157
- end
158
- new_way_to_build_xml(builder) # compare to new way
159
- end
160
-
161
- def known_good_way_to_build_xml(builder)
162
- builder.person
163
- end
164
-
165
- def new_way_to_build_xml(builder)
166
- builder.person
167
- end
168
-
169
- end
170
-
171
- describe "Multiple Return Values" do
172
-
173
- # Expect multiple calls, returning a different value each time
174
-
175
- # Sometimes you need to return different values for each call to a
176
- # mocked method. This example shifts values out of a list for this
177
- # effect.
178
-
179
- it "returns multiple values" do
180
- file = flexmock('file')
181
- file.should_receive(:gets).with_no_args.
182
- and_return("line 1\n", "line 2\n")
183
-
184
- # test code here
185
-
186
- file.gets # returns "line 1"
187
- file.gets # returns "line 2"
188
- end
189
- end
190
-
191
- describe "Ignore Unimportant Messages" do
192
-
193
- # Ignore uninteresting messages
194
-
195
- # Generally you need to mock only those methods that return an
196
- # interesting value or wish to assert were sent in a particular
197
- # manner. Use the <code>should_ignore_missing</code> method to turn
198
- # on missing method ignoring.
199
-
200
- it "ignores unimportant messages" do
201
- m = flexmock('m')
202
- m.should_receive(:an_important_message).and_return(1).once
203
- m.should_ignore_missing
204
-
205
- # Test Code
206
-
207
- m.an_important_message
208
- m.an_unimportant_message
209
- end
210
-
211
- # When <code>should_ignore_missing</code> is enabled, ignored
212
- # missing methods will return an undefined object. Any operation on
213
- # the undefined object will return the undefined object.
214
-
215
- end
216
-
217
-
218
- describe "Partial Mocks" do
219
-
220
- # Mock just one method on an existing object
221
-
222
- # The Portfolio class calculate the value of a set of stocks by
223
- # talking to a quote service via a web service. Since we don't want
224
- # to use a real web service in our unit tests, we will mock the
225
- # quote service.
226
-
227
- it "returns the portfolio value" do
228
- flexmock(QuoteService).new_instances do |m|
229
- m.should_receive(:quote).and_return(100)
230
- end
231
- port = Portfolio.new
232
- value = port.value # Portfolio calls QuoteService.quote
233
- value.should == 100
234
- end
235
-
236
- class QuoteService
237
- end
238
-
239
- class Portfolio
240
- def value
241
- qs = QuoteService.new
242
- qs.quote
243
- end
244
- end
245
- end