flexmock 0.9.0 → 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/README.rdoc +325 -177
  2. data/Rakefile +22 -8
  3. data/TAGS +772 -669
  4. data/doc/releases/flexmock-1.0.0.rdoc +128 -0
  5. data/lib/flexmock.rb +1 -1
  6. data/lib/flexmock/argument_matchers.rb +16 -3
  7. data/lib/flexmock/argument_matching.rb +33 -0
  8. data/lib/flexmock/argument_types.rb +5 -1
  9. data/lib/flexmock/base.rb +1 -1
  10. data/lib/flexmock/composite.rb +3 -3
  11. data/lib/flexmock/core.rb +32 -2
  12. data/lib/flexmock/core_class_methods.rb +1 -1
  13. data/lib/flexmock/default_framework_adapter.rb +1 -1
  14. data/lib/flexmock/deprecated_methods.rb +1 -1
  15. data/lib/flexmock/errors.rb +1 -1
  16. data/lib/flexmock/expectation.rb +15 -13
  17. data/lib/flexmock/expectation_director.rb +1 -1
  18. data/lib/flexmock/explicit_needed.rb +39 -0
  19. data/lib/flexmock/mock_container.rb +8 -2
  20. data/lib/flexmock/noop.rb +1 -1
  21. data/lib/flexmock/ordering.rb +1 -1
  22. data/lib/flexmock/partial_mock.rb +12 -2
  23. data/lib/flexmock/rails.rb +1 -1
  24. data/lib/flexmock/recorder.rb +1 -1
  25. data/lib/flexmock/rspec.rb +5 -1
  26. data/lib/flexmock/rspec_spy_matcher.rb +74 -0
  27. data/lib/flexmock/spy_describers.rb +60 -0
  28. data/lib/flexmock/test_unit.rb +1 -1
  29. data/lib/flexmock/test_unit_assert_spy_called.rb +34 -0
  30. data/lib/flexmock/test_unit_integration.rb +3 -1
  31. data/lib/flexmock/undefined.rb +1 -1
  32. data/lib/flexmock/validators.rb +1 -1
  33. data/lib/flexmock/version.rb +4 -2
  34. data/test/assert_spy_called_test.rb +89 -0
  35. data/test/container_methods_test.rb +1 -1
  36. data/test/default_framework_adapter_test.rb +1 -1
  37. data/test/deprecated_methods_test.rb +1 -1
  38. data/test/examples_from_readme_test.rb +1 -1
  39. data/test/extended_should_receive_test.rb +1 -1
  40. data/test/naming_test.rb +1 -2
  41. data/test/new_instances_test.rb +1 -3
  42. data/test/partial_mock_test.rb +7 -1
  43. data/test/record_mode_test.rb +1 -1
  44. data/test/rspec_integration/integration_spec.rb +11 -3
  45. data/test/rspec_integration/spy_example_spec.rb +141 -0
  46. data/test/samples_test.rb +1 -1
  47. data/test/should_ignore_missing_test.rb +6 -2
  48. data/test/should_receive_test.rb +31 -2
  49. data/test/spys_test.rb +148 -0
  50. data/test/test_unit_integration/auto_test_unit_test.rb +1 -1
  51. data/test/tu_integration_test.rb +1 -1
  52. data/test/undefined_test.rb +1 -1
  53. metadata +16 -8
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
data/test/naming_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -82,4 +82,3 @@ class TestNaming < Test::Unit::TestCase
82
82
  assert_equal "MOCK-INSPECT", dummy.inspect
83
83
  end
84
84
  end
85
-
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -213,5 +213,3 @@ class TestNewInstances < Test::Unit::TestCase
213
213
  dog.bark
214
214
  end
215
215
  end
216
-
217
-
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -61,6 +61,12 @@ class TestStubbing < Test::Unit::TestCase
61
61
  assert_equal flexmock(obj), flexmock(obj)
62
62
  end
63
63
 
64
+ def test_stubbed_methods_can_invoke_original_behavior
65
+ dog = Dog.new
66
+ flexmock(dog).should_receive(:bark).pass_thru.once
67
+ assert_equal :woof, dog.bark
68
+ end
69
+
64
70
  def test_multiple_methods_can_be_stubbed
65
71
  dog = Dog.new
66
72
  flexmock(dog).should_receive(:bark).and_return(:grrrr)
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -27,6 +27,9 @@ describe "FlexMock in a RSpec example" do
27
27
  specify "should have an error when a mock is not called" do
28
28
  m = flexmock("Expectation Failured")
29
29
  m.should_receive(:hi).with().once
30
+ lambda {
31
+ flexmock_verify
32
+ }.should raise_error(RSpec::Expectations::ExpectationNotMetError, /method 'hi\(\)' called incorrect number of times/)
30
33
  end
31
34
 
32
35
  specify "should be able to create a stub" do
@@ -37,12 +40,17 @@ describe "FlexMock in a RSpec example" do
37
40
  end
38
41
 
39
42
  specify "Should show an example failure" do
40
- 1.should == 2
43
+ lambda {
44
+ 1.should == 2
45
+ }.should raise_error(RSpec::Expectations::ExpectationNotMetError,
46
+ /expected: 2.*got: 1/m)
41
47
  end
42
48
 
43
49
  specify "Should show how mocks are displayed in error messages" do
44
50
  m = flexmock("x")
45
- m.should == 2
51
+ lambda {
52
+ m.should == 2
53
+ }.should raise_error(RSpec::Expectations::ExpectationNotMetError, /got: <FlexMock:x>/)
46
54
  end
47
55
 
48
56
  end
@@ -0,0 +1,141 @@
1
+ require 'flexmock/rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :flexmock
5
+ end
6
+
7
+ describe "Dog" do
8
+ class Dog
9
+ def wags(arg)
10
+ fail "SHOULD NOT BE CALLED"
11
+ end
12
+
13
+ def barks
14
+ fail "SHOULD NOT BE CALLED"
15
+ end
16
+ end
17
+
18
+ let(:dog) { flexmock(:on, Dog) }
19
+
20
+ context "single calls with arguments" do
21
+ before do
22
+ dog.wags(:tail)
23
+ end
24
+
25
+ it "wags the tail" do
26
+ dog.should have_received(:wags).with(:tail)
27
+ end
28
+
29
+ it "does not bark the tail" do
30
+ dog.should_not have_received(:bark)
31
+ end
32
+
33
+ it "rejects wag(:foot)" do
34
+ should_fail(/^expected wag\(:foot\) to be called on <FlexMock:Dog Mock>/i) do
35
+ dog.should have_received(:wag).with(:foot)
36
+ end
37
+ end
38
+
39
+ it "rejects not wag(:tail)" do
40
+ should_fail(/^expected wag\(:foot\) to be called on <FlexMock:Dog Mock>/i) do
41
+ dog.should_not have_received(:wag).with(:tail)
42
+ end
43
+ end
44
+ end
45
+
46
+ context "multiple calls with multiple arguments" do
47
+ before do
48
+ dog.wags(:tail)
49
+ dog.wags(:tail)
50
+ dog.wags(:tail)
51
+ dog.wags(:head, :tail)
52
+ dog.barks
53
+ dog.barks
54
+ end
55
+
56
+ it "accepts wags(:tail) multiple times" do
57
+ dog.should have_received(:wags).with(:tail).times(3)
58
+ end
59
+
60
+ it "rejects wags(:tail) wrong times value" do
61
+ should_fail(/^expected wags\(:tail\) to be called on <FlexMock:Dog Mock>/i) do
62
+ dog.should have_received(:wags).with(:tail).times(2)
63
+ end
64
+ end
65
+
66
+ it "detects any_args" do
67
+ dog.should have_received(:wags).times(4)
68
+ end
69
+
70
+ it "accepts once" do
71
+ dog.should have_received(:wags).with(:head, :tail).once
72
+ end
73
+
74
+ it "rejects an incorrect once" do
75
+ should_fail(/^expected wags\(:tail\) to be called on <FlexMock:Dog Mock> once/i) do
76
+ dog.should have_received(:wags).with(:tail).once
77
+ end
78
+ end
79
+
80
+ it "accepts twice" do
81
+ dog.should have_received(:barks).twice
82
+ end
83
+
84
+ it "rejects an incorrect twice" do
85
+ should_fail(/^expected wags\(:tail\) to be called on <FlexMock:Dog Mock> twice/) do
86
+ dog.should have_received(:wags).with(:tail).twice
87
+ end
88
+ end
89
+
90
+ it "accepts never" do
91
+ dog.should have_received(:jump).never
92
+ end
93
+
94
+ it "rejects an incorrect never" do
95
+ should_fail(/^expected barks\(\) to be called on <FlexMock:Dog Mock> never/i) do
96
+ dog.should have_received(:barks).with().never
97
+ end
98
+ end
99
+
100
+ it "rejects an incorrect never" do
101
+ dog.should_not have_received(:jump)
102
+ end
103
+ end
104
+
105
+ context "with blocks" do
106
+ before do
107
+ dog.wags { }
108
+ dog.barks
109
+ end
110
+
111
+ it "accepts wags with a block" do
112
+ dog.should have_received(:wags).with_a_block
113
+ dog.should have_received(:wags)
114
+ end
115
+
116
+ it "accepts barks without a block" do
117
+ dog.should have_received(:barks).without_a_block
118
+ dog.should have_received(:barks)
119
+ end
120
+
121
+ it "rejects wags without a block" do
122
+ should_fail(/without a block/) do
123
+ dog.should have_received(:wags).without_a_block
124
+ end
125
+ end
126
+
127
+ it "rejects barks with a block" do
128
+ should_fail(/with a block/) do
129
+ dog.should have_received(:wags).with_a_block
130
+ end
131
+ end
132
+ end
133
+
134
+ def should_fail(message_pattern)
135
+ begin
136
+ yield
137
+ rescue RSpec::Expectations::ExpectationNotMetError => ex
138
+ ex.message.should match(message_pattern)
139
+ end
140
+ end
141
+ end
data/test/samples_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -32,6 +32,11 @@ class TestShouldIgnoreMissing < Test::Unit::TestCase
32
32
  assert @mock.respond_to?(:unknown_foo)
33
33
  end
34
34
 
35
+ def test_should_ignore_missing_returns_mock
36
+ result = @mock.should_ignore_missing
37
+ assert_equal result, @mock
38
+ end
39
+
35
40
  def test_ignored_methods_return_undefined
36
41
  @mock.should_ignore_missing
37
42
  assert_equal FlexMock.undefined, @mock.unknown_foo
@@ -77,4 +82,3 @@ class TestShouldIgnoreMissing < Test::Unit::TestCase
77
82
  assert_equal FlexMock.undefined, method_proc.call
78
83
  end
79
84
  end
80
-
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -301,6 +301,13 @@ class TestFlexMockShoulds < Test::Unit::TestCase
301
301
  end
302
302
  end
303
303
 
304
+ def test_pass_thru_just_returns_undefined_on_mocks
305
+ FlexMock.use do |m|
306
+ m.should_receive(:hi).pass_thru
307
+ assert_equal FlexMock.undefined, m.hi
308
+ end
309
+ end
310
+
304
311
  def test_multiple_expectations
305
312
  FlexMock.use do |m|
306
313
  m.should_receive(:hi).with(1).returns(10)
@@ -403,9 +410,31 @@ class TestFlexMockShoulds < Test::Unit::TestCase
403
410
  end
404
411
  end
405
412
 
413
+ def test_with_optional_proc
414
+ FlexMock.use('greeter') do |m|
415
+ m.should_receive(:hi).with(optional_proc).once
416
+ m.hi { }
417
+ end
418
+ end
419
+
420
+ def test_with_optional_proc_and_missing_proc
421
+ FlexMock.use('greeter') do |m|
422
+ m.should_receive(:hi).with(optional_proc).once
423
+ m.hi
424
+ end
425
+ end
426
+
427
+ def test_with_optional_proc_distinquishes_between_nil_and_missing
428
+ FlexMock.use('greeter') do |m|
429
+ m.should_receive(:hi).with(optional_proc).never
430
+ m.should_receive(:hi).with(nil).once
431
+ m.hi(nil)
432
+ end
433
+ end
434
+
406
435
  def test_with_arbitrary_arg_matching
407
436
  FlexMock.use('greeter') do |m|
408
- m.should_receive(:hi).with(FlexMock.on { |arg| arg % 2 == 0 }).twice
437
+ m.should_receive(:hi).with(FlexMock.on { |arg| arg % 2 == 0 rescue nil }).twice
409
438
  m.should_receive(:hi).never
410
439
  m.should_receive(:hi).with(1).once
411
440
  m.should_receive(:hi).with(2).never
data/test/spys_test.rb ADDED
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/test_setup'
4
+ require 'flexmock/spy_describers'
5
+
6
+ class TestSpys < Test::Unit::TestCase
7
+ include FlexMock::TestCase
8
+
9
+ class FooBar
10
+ def foo
11
+ end
12
+ def bar
13
+ end
14
+ end
15
+
16
+ def setup
17
+ super
18
+ @spy = flexmock(:on, FooBar)
19
+ end
20
+
21
+ def test_spy_detects_simple_call
22
+ @spy.foo
23
+ assert_spy_called @spy, :foo
24
+ end
25
+
26
+ def test_spy_detects_simple_call_ignoring_args
27
+ @spy.foo(1)
28
+ assert_spy_called @spy, :foo, :_
29
+ end
30
+
31
+ def test_spy_rejects_a_never_made_call
32
+ @spy.foo
33
+ assert_spy_not_called @spy, :bar
34
+ end
35
+
36
+ def test_spy_detects_call_with_literal_arg
37
+ @spy.foo(1)
38
+ assert_spy_called @spy, :foo, 1
39
+ end
40
+
41
+ def test_spy_detects_call_with_class_arg
42
+ @spy.foo(1)
43
+ assert_spy_called @spy, :foo, Integer
44
+ end
45
+
46
+ def test_spy_rejects_call_with_non_matching_literal_arg
47
+ @spy.foo(2)
48
+ assert_spy_not_called @spy, :foo, 1
49
+ end
50
+
51
+ def test_spy_detects_call_with_multiple_arguments
52
+ @spy.foo(1, "HI", :foo)
53
+ assert_spy_called @spy, :foo, /1/, "HI", Symbol
54
+ end
55
+
56
+ def test_spy_detects_multiple_calls_with_different_arguments
57
+ @spy.foo(1)
58
+ @spy.foo(1)
59
+ assert_spy_called @spy, {times: 2}, :foo, 1
60
+ end
61
+
62
+ def test_spy_rejects_if_times_options_not_matching
63
+ @spy.foo(1)
64
+ @spy.foo(1)
65
+ assert_spy_not_called @spy, {:times => 1}, :foo, 1
66
+ end
67
+
68
+ def test_spy_detects_a_block
69
+ @spy.foo { }
70
+ assert_spy_called @spy, :foo, Proc
71
+ end
72
+
73
+ def test_spy_rejects_a_block
74
+ @spy.foo { }
75
+ assert_spy_not_called @spy, {with_block: false}, :foo
76
+ end
77
+
78
+ def test_spy_detects_a_missing_block
79
+ @spy.foo
80
+ assert_spy_called @spy, {with_block: false}, :foo
81
+ end
82
+
83
+ def test_spy_rejects_a_missing_block
84
+ @spy.foo
85
+ assert_spy_not_called @spy, :foo, Proc
86
+ end
87
+
88
+ def test_spy_ignores_block
89
+ @spy.foo { }
90
+ assert_spy_called @spy, :foo, Proc
91
+ end
92
+
93
+ def test_spy_methods_can_be_stubbed
94
+ @spy.should_receive(:foo).and_return(:hi)
95
+ result = @spy.foo
96
+ assert_equal result, :hi
97
+ assert @spy.flexmock_was_called_with?(:foo, nil)
98
+ end
99
+
100
+ def test_calling_non_spy_base_methods_is_an_error
101
+ assert_raise(NoMethodError) do
102
+ @spy.baz
103
+ end
104
+ end
105
+
106
+ def test_cant_put_expectations_on_non_base_class_methodsx
107
+ ex = assert_raise(NoMethodError) do
108
+ exp = @spy.should_receive(:baz).and_return(:bar)
109
+ end
110
+ assert_match(/cannot stub.*defined.*base.*class/i, ex.message)
111
+ assert_match(/method: +baz/i, ex.message)
112
+ assert_match(/base class: +TestSpys::FooBar/i, ex.message)
113
+ end
114
+
115
+ def test_cant_put_expectations_on_non_base_class_methods_unless_explicit
116
+ exp = @spy.should_receive(:baz).explicit.and_return(:bar)
117
+ @spy.baz
118
+ assert_spy_called @spy, :baz
119
+ end
120
+
121
+ def test_ok_to_use_explicit_even_when_its_not_needed
122
+ exp = @spy.should_receive(:foo).explicit.and_return(:bar)
123
+ @spy.foo
124
+ assert_spy_called @spy, :foo
125
+ end
126
+
127
+ def test_can_spy_on_partial_mocks
128
+ @foo = FooBar.new
129
+ @spy = flexmock(@foo)
130
+ @foo.should_receive(:foo => :baz)
131
+ result = @foo.foo
132
+ assert_equal :baz, result
133
+ assert_spy_called @foo, :foo
134
+ end
135
+
136
+ def test_can_spy_on_class_methods
137
+ flexmock(FooBar).should_receive(:new).and_return(:dummy)
138
+ FooBar.new
139
+ assert_spy_called FooBar, :new,
140
+ end
141
+
142
+ def test_can_spy_on_regular_mocks
143
+ mock = flexmock("regular mock")
144
+ mock.should_receive(:foo => :bar)
145
+ mock.foo
146
+ assert_spy_called mock, :foo
147
+ end
148
+ end