rspec-mocks 2.10.0 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/Changelog.md +36 -1
  2. data/README.md +14 -2
  3. data/features/stubbing_constants/README.md +62 -0
  4. data/features/stubbing_constants/stub_defined_constant.feature +79 -0
  5. data/features/stubbing_constants/stub_undefined_constant.feature +50 -0
  6. data/lib/rspec/mocks/any_instance/chain.rb +0 -81
  7. data/lib/rspec/mocks/any_instance/expectation_chain.rb +57 -0
  8. data/lib/rspec/mocks/any_instance/recorder.rb +6 -1
  9. data/lib/rspec/mocks/any_instance/stub_chain.rb +37 -0
  10. data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +25 -0
  11. data/lib/rspec/mocks/any_instance.rb +37 -1
  12. data/lib/rspec/mocks/argument_list_matcher.rb +93 -0
  13. data/lib/rspec/mocks/argument_matchers.rb +39 -31
  14. data/lib/rspec/mocks/error_generator.rb +7 -0
  15. data/lib/rspec/mocks/example_methods.rb +41 -0
  16. data/lib/rspec/mocks/framework.rb +2 -1
  17. data/lib/rspec/mocks/message_expectation.rb +27 -15
  18. data/lib/rspec/mocks/methods.rb +4 -0
  19. data/lib/rspec/mocks/proxy.rb +10 -4
  20. data/lib/rspec/mocks/stub_const.rb +280 -0
  21. data/lib/rspec/mocks/test_double.rb +3 -2
  22. data/lib/rspec/mocks/version.rb +1 -1
  23. data/spec/rspec/mocks/any_instance/message_chains_spec.rb +1 -1
  24. data/spec/rspec/mocks/any_instance_spec.rb +66 -26
  25. data/spec/rspec/mocks/argument_expectation_spec.rb +7 -7
  26. data/spec/rspec/mocks/at_least_spec.rb +89 -50
  27. data/spec/rspec/mocks/block_return_value_spec.rb +8 -0
  28. data/spec/rspec/mocks/mock_spec.rb +261 -246
  29. data/spec/rspec/mocks/multiple_return_value_spec.rb +2 -2
  30. data/spec/rspec/mocks/null_object_mock_spec.rb +22 -0
  31. data/spec/rspec/mocks/stub_chain_spec.rb +47 -47
  32. data/spec/rspec/mocks/stub_const_spec.rb +309 -0
  33. data/spec/rspec/mocks/stub_spec.rb +2 -2
  34. data/spec/spec_helper.rb +3 -41
  35. metadata +18 -6
  36. data/lib/rspec/mocks/argument_expectation.rb +0 -52
@@ -3,100 +3,139 @@ require 'spec_helper'
3
3
  module RSpec
4
4
  module Mocks
5
5
  describe "at_least" do
6
- before(:each) do
7
- @mock = RSpec::Mocks::Mock.new("test mock")
8
- end
6
+ before(:each) { @double = double }
9
7
 
10
8
  it "fails if method is never called" do
11
- @mock.should_receive(:random_call).at_least(4).times
12
- lambda do
13
- @mock.rspec_verify
9
+ @double.should_receive(:do_something).at_least(4).times
10
+ lambda do
11
+ @double.rspec_verify
14
12
  end.should raise_error(RSpec::Mocks::MockExpectationError)
15
13
  end
16
14
 
17
15
  it "fails when called less than n times" do
18
- @mock.should_receive(:random_call).at_least(4).times
19
- @mock.random_call
20
- @mock.random_call
21
- @mock.random_call
16
+ @double.should_receive(:do_something).at_least(4).times
17
+ @double.do_something
18
+ @double.do_something
19
+ @double.do_something
22
20
  lambda do
23
- @mock.rspec_verify
21
+ @double.rspec_verify
24
22
  end.should raise_error(RSpec::Mocks::MockExpectationError)
25
23
  end
26
24
 
27
25
  it "fails when at least once method is never called" do
28
- @mock.should_receive(:random_call).at_least(:once)
26
+ @double.should_receive(:do_something).at_least(:once)
29
27
  lambda do
30
- @mock.rspec_verify
28
+ @double.rspec_verify
31
29
  end.should raise_error(RSpec::Mocks::MockExpectationError)
32
30
  end
33
31
 
34
32
  it "fails when at least twice method is called once" do
35
- @mock.should_receive(:random_call).at_least(:twice)
36
- @mock.random_call
33
+ @double.should_receive(:do_something).at_least(:twice)
34
+ @double.do_something
37
35
  lambda do
38
- @mock.rspec_verify
36
+ @double.rspec_verify
39
37
  end.should raise_error(RSpec::Mocks::MockExpectationError)
40
38
  end
41
39
 
42
40
  it "fails when at least twice method is never called" do
43
- @mock.should_receive(:random_call).at_least(:twice)
41
+ @double.should_receive(:do_something).at_least(:twice)
44
42
  lambda do
45
- @mock.rspec_verify
43
+ @double.rspec_verify
46
44
  end.should raise_error(RSpec::Mocks::MockExpectationError)
47
45
  end
48
46
 
49
47
  it "passes when at least n times method is called exactly n times" do
50
- @mock.should_receive(:random_call).at_least(4).times
51
- @mock.random_call
52
- @mock.random_call
53
- @mock.random_call
54
- @mock.random_call
55
- @mock.rspec_verify
48
+ @double.should_receive(:do_something).at_least(4).times
49
+ @double.do_something
50
+ @double.do_something
51
+ @double.do_something
52
+ @double.do_something
53
+ @double.rspec_verify
56
54
  end
57
55
 
58
56
  it "passes when at least n times method is called n plus 1 times" do
59
- @mock.should_receive(:random_call).at_least(4).times
60
- @mock.random_call
61
- @mock.random_call
62
- @mock.random_call
63
- @mock.random_call
64
- @mock.random_call
65
- @mock.rspec_verify
57
+ @double.should_receive(:do_something).at_least(4).times
58
+ @double.do_something
59
+ @double.do_something
60
+ @double.do_something
61
+ @double.do_something
62
+ @double.do_something
63
+ @double.rspec_verify
66
64
  end
67
65
 
68
66
  it "passes when at least once method is called once" do
69
- @mock.should_receive(:random_call).at_least(:once)
70
- @mock.random_call
71
- @mock.rspec_verify
67
+ @double.should_receive(:do_something).at_least(:once)
68
+ @double.do_something
69
+ @double.rspec_verify
72
70
  end
73
71
 
74
72
  it "passes when at least once method is called twice" do
75
- @mock.should_receive(:random_call).at_least(:once)
76
- @mock.random_call
77
- @mock.random_call
78
- @mock.rspec_verify
73
+ @double.should_receive(:do_something).at_least(:once)
74
+ @double.do_something
75
+ @double.do_something
76
+ @double.rspec_verify
79
77
  end
80
78
 
81
79
  it "passes when at least twice method is called three times" do
82
- @mock.should_receive(:random_call).at_least(:twice)
83
- @mock.random_call
84
- @mock.random_call
85
- @mock.random_call
86
- @mock.rspec_verify
80
+ @double.should_receive(:do_something).at_least(:twice)
81
+ @double.do_something
82
+ @double.do_something
83
+ @double.do_something
84
+ @double.rspec_verify
87
85
  end
88
86
 
89
87
  it "passes when at least twice method is called twice" do
90
- @mock.should_receive(:random_call).at_least(:twice)
91
- @mock.random_call
92
- @mock.random_call
93
- @mock.rspec_verify
88
+ @double.should_receive(:do_something).at_least(:twice)
89
+ @double.do_something
90
+ @double.do_something
91
+ @double.rspec_verify
94
92
  end
95
93
 
96
94
  it "returns the value given by a block when the at least once method is called" do
97
- @mock.should_receive(:to_s).at_least(:once) { "testing" }
98
- @mock.to_s.should eq "testing"
99
- @mock.rspec_verify
95
+ @double.should_receive(:to_s).at_least(:once) { "testing" }
96
+ @double.to_s.should eq "testing"
97
+ @double.rspec_verify
98
+ end
99
+
100
+ it "passes with at_least(0) with no return if called once" do
101
+ @double.should_receive(:do_something).at_least(0).times
102
+ @double.do_something
103
+ end
104
+
105
+ it "passes with at_least(0) with return block if called once" do
106
+ @double.should_receive(:do_something).at_least(0).times { true }
107
+ @double.do_something
108
+ end
109
+
110
+ it "passes with at_least(0) with and_return if called once" do
111
+ @double.should_receive(:do_something).at_least(0).times.and_return true
112
+ @double.do_something
113
+ end
114
+
115
+ it "passes with at_least(0) with no return if never called" do
116
+ @double.should_receive(:do_something).at_least(0).times
117
+ end
118
+
119
+ it "passes with at_least(0) with return block if never called" do
120
+ @double.should_receive(:do_something).at_least(0).times { true }
121
+ end
122
+
123
+ it "passes with at_least(0) with and_return if never called" do
124
+ @double.should_receive(:do_something).at_least(0).times.and_return true
125
+ end
126
+
127
+ it "uses a stub value if no value set" do
128
+ @double.stub(:do_something => 'foo')
129
+ @double.should_receive(:do_something).at_least(:once)
130
+ @double.do_something.should eq 'foo'
131
+ @double.do_something.should eq 'foo'
132
+ end
133
+
134
+ it "prefers its own return value over a stub" do
135
+ @double.stub(:do_something => 'foo')
136
+ @double.should_receive(:do_something).at_least(:once).and_return('bar')
137
+ @double.do_something.should eq 'bar'
138
+ @double.do_something.should eq 'bar'
100
139
  end
101
140
  end
102
141
  end
@@ -7,6 +7,14 @@ describe "a double declaration with a block handed to:" do
7
7
  obj.should_receive(:foo) { 'bar' }
8
8
  obj.foo.should eq('bar')
9
9
  end
10
+
11
+ it "works when a multi-return stub has already been set" do
12
+ obj = Object.new
13
+ return_value = Object.new
14
+ obj.stub(:foo).and_return(return_value, nil)
15
+ obj.should_receive(:foo) { return_value }
16
+ obj.foo.should be(return_value)
17
+ end
10
18
  end
11
19
 
12
20
  describe "stub" do