rspec-mocks 2.0.0.beta.4 → 2.0.0.beta.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +3 -2
- data/VERSION +1 -1
- data/features/mocks/block_local_expectations.feature +0 -14
- data/features/mocks/mix_stubs_and_mocks.feature +2 -2
- data/features/mocks/warn_when_expectation_is_set_on_nil.feature +50 -0
- data/lib/rspec/mocks.rb +9 -9
- data/lib/rspec/mocks/argument_matchers.rb +1 -1
- data/lib/rspec/mocks/error_generator.rb +24 -8
- data/lib/rspec/mocks/framework.rb +1 -0
- data/lib/rspec/mocks/message_expectation.rb +1 -1
- data/lib/rspec/mocks/method_double.rb +156 -0
- data/lib/rspec/mocks/methods.rb +2 -2
- data/lib/rspec/mocks/mock.rb +17 -10
- data/lib/rspec/mocks/proxy.rb +71 -158
- data/lib/rspec/mocks/spec_methods.rb +19 -3
- data/rspec-mocks.gemspec +14 -10
- data/spec/rspec/mocks/any_number_of_times_spec.rb +1 -1
- data/spec/rspec/mocks/argument_expectation_spec.rb +2 -2
- data/spec/rspec/mocks/bug_report_10260_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_10263_spec.rb +8 -8
- data/spec/rspec/mocks/bug_report_15719_spec.rb +12 -12
- data/spec/rspec/mocks/bug_report_7611_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_7805_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_8165_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_957_spec.rb +1 -1
- data/spec/rspec/mocks/double_spec.rb +12 -0
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +7 -6
- data/spec/rspec/mocks/mock_ordering_spec.rb +54 -54
- data/spec/rspec/mocks/mock_space_spec.rb +4 -4
- data/spec/rspec/mocks/mock_spec.rb +55 -51
- data/spec/rspec/mocks/multiple_return_value_spec.rb +29 -7
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +8 -8
- data/spec/rspec/mocks/null_object_mock_spec.rb +2 -2
- data/spec/rspec/mocks/once_counts_spec.rb +1 -1
- data/spec/rspec/mocks/options_hash_spec.rb +1 -1
- data/spec/rspec/mocks/partial_mock_spec.rb +13 -7
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +1 -1
- data/spec/rspec/mocks/precise_counts_spec.rb +1 -1
- data/spec/rspec/mocks/record_messages_spec.rb +1 -1
- data/spec/rspec/mocks/stub_spec.rb +22 -22
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +12 -12
- data/spec/rspec/mocks/twice_counts_spec.rb +1 -1
- metadata +13 -9
@@ -1,26 +1,26 @@
|
|
1
1
|
describe "Mock" do
|
2
2
|
before do
|
3
|
-
@
|
3
|
+
@double = double("test double")
|
4
4
|
end
|
5
5
|
|
6
6
|
specify "when one example has an expectation (non-mock) inside the block passed to the mock" do
|
7
|
-
@
|
7
|
+
@double.should_receive(:msg) do |b|
|
8
8
|
b.should be_true #this call exposes the problem
|
9
9
|
end
|
10
10
|
begin
|
11
|
-
@
|
11
|
+
@double.msg(false)
|
12
12
|
rescue Exception
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
specify "then the next example should behave as expected instead of saying" do
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
17
|
+
@double.should_receive(:foobar)
|
18
|
+
@double.foobar
|
19
|
+
@double.rspec_verify
|
20
20
|
begin
|
21
|
-
@
|
21
|
+
@double.foobar
|
22
22
|
rescue Exception => e
|
23
|
-
e.message.should == "
|
23
|
+
e.message.should == "Double \"test double\" received unexpected message :foobar with (no args)"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -5,25 +5,25 @@ module Rspec
|
|
5
5
|
describe "mock failure" do
|
6
6
|
|
7
7
|
it "should tell you when it receives the right message with the wrong args" do
|
8
|
-
|
9
|
-
|
8
|
+
double = double("foo")
|
9
|
+
double.should_receive(:bar).with("message")
|
10
10
|
lambda {
|
11
|
-
|
12
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, %Q{
|
13
|
-
|
11
|
+
double.bar("different message")
|
12
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, %Q{Double "foo" received :bar with unexpected arguments\n expected: ("message")\n got: ("different message")})
|
13
|
+
double.rspec_reset # so the example doesn't fail
|
14
14
|
end
|
15
15
|
|
16
16
|
pending "should tell you when it receives the right message with the wrong args if you stub the method (fix bug 15719)" do
|
17
17
|
# NOTE - for whatever reason, if you use a the block style of pending here,
|
18
18
|
# rcov gets unhappy. Don't know why yet.
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
double = double("foo")
|
20
|
+
double.stub(:bar)
|
21
|
+
double.should_receive(:bar).with("message")
|
22
22
|
lambda {
|
23
|
-
|
24
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, %Q{
|
25
|
-
|
23
|
+
double.bar("different message")
|
24
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, %Q{Double 'foo' expected :bar with ("message") but received it with ("different message")})
|
25
|
+
double.rspec_reset # so the example doesn't fail
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end
|
@@ -5,7 +5,7 @@ module Bug7805
|
|
5
5
|
|
6
6
|
describe "Stubs should correctly restore module methods" do
|
7
7
|
it "1 - stub the open method" do
|
8
|
-
File.stub
|
8
|
+
File.stub(:open).and_return("something")
|
9
9
|
File.open.should == "something"
|
10
10
|
end
|
11
11
|
it "2 - use File.open to create example.txt" do
|
@@ -21,7 +21,7 @@ describe "An object where respond_to? is true and does not have method" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should not raise an exception for mock" do
|
24
|
-
obj =
|
24
|
+
obj = double("obj")
|
25
25
|
obj.should_receive(:respond_to?).with(:foobar).and_return(true)
|
26
26
|
obj.should_receive(:foobar).and_return(:baz)
|
27
27
|
obj.respond_to?(:foobar).should be_true
|
@@ -7,7 +7,7 @@ module Spec
|
|
7
7
|
@base_class = Class.new
|
8
8
|
@concrete_class = Class.new(@base_class)
|
9
9
|
|
10
|
-
@base_class.stub
|
10
|
+
@base_class.stub(:find).and_return "stubbed_value"
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return the value for the stub on the base class" do
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "double" do
|
4
|
+
it "is an alias for stub and mock" do
|
5
|
+
double().should be_a(Rspec::Mocks::Mock)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "uses 'Double' in failure messages" do
|
9
|
+
double = double('name')
|
10
|
+
expect {double.foo}.to raise_error(/Double "name" received/)
|
11
|
+
end
|
12
|
+
end
|
@@ -4,7 +4,7 @@ module Rspec
|
|
4
4
|
module Mocks
|
5
5
|
describe "failing MockArgumentMatchers" do
|
6
6
|
before(:each) do
|
7
|
-
@mock =
|
7
|
+
@mock = double("test double")
|
8
8
|
@reporter = Rspec::Mocks::Mock.new("reporter", :null_object => true)
|
9
9
|
end
|
10
10
|
|
@@ -52,35 +52,35 @@ module Rspec
|
|
52
52
|
@mock.should_receive(:random_call).with(:a => "b", :c => "d")
|
53
53
|
lambda do
|
54
54
|
@mock.random_call(:a => "b", :c => "e")
|
55
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, /
|
55
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, /Double "test double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(:a=>\"b\", :c=>\"e\"|:c=>\"e\", :a=>\"b\")\}\)/)
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should fail for a hash w/ wrong keys" do
|
59
59
|
@mock.should_receive(:random_call).with(:a => "b", :c => "d")
|
60
60
|
lambda do
|
61
61
|
@mock.random_call("a" => "b", "c" => "d")
|
62
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, /
|
62
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, /Double "test double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(\"a\"=>\"b\", \"c\"=>\"d\"|\"c\"=>\"d\", \"a\"=>\"b\")\}\)/)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should match against a Matcher" do
|
66
66
|
lambda do
|
67
67
|
@mock.should_receive(:msg).with(equal(3))
|
68
68
|
@mock.msg(37)
|
69
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, "
|
69
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :msg with unexpected arguments\n expected: (equal 3)\n got: (37)")
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should fail no_args with one arg" do
|
73
73
|
lambda do
|
74
74
|
@mock.should_receive(:msg).with(no_args)
|
75
75
|
@mock.msg(37)
|
76
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, "
|
76
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :msg with unexpected arguments\n expected: (no args)\n got: (37)")
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should fail hash_including with missing key" do
|
80
80
|
lambda do
|
81
81
|
@mock.should_receive(:msg).with(hash_including(:a => 1))
|
82
82
|
@mock.msg({})
|
83
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, "
|
83
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :msg with unexpected arguments\n expected: (hash_including(:a=>1))\n got: ({})")
|
84
84
|
end
|
85
85
|
|
86
86
|
it "should fail with block matchers" do
|
@@ -93,3 +93,4 @@ module Rspec
|
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
96
|
+
|
@@ -3,90 +3,90 @@ require 'spec_helper'
|
|
3
3
|
module Rspec
|
4
4
|
module Mocks
|
5
5
|
|
6
|
-
describe "
|
6
|
+
describe "ordering" do
|
7
7
|
|
8
8
|
before do
|
9
|
-
@
|
9
|
+
@double = double("test double")
|
10
10
|
end
|
11
11
|
|
12
12
|
after do
|
13
|
-
@
|
13
|
+
@double.rspec_reset
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should pass two calls in order" do
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
17
|
+
@double.should_receive(:one).ordered
|
18
|
+
@double.should_receive(:two).ordered
|
19
|
+
@double.one
|
20
|
+
@double.two
|
21
|
+
@double.rspec_verify
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should pass three calls in order" do
|
25
|
-
@
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
25
|
+
@double.should_receive(:one).ordered
|
26
|
+
@double.should_receive(:two).ordered
|
27
|
+
@double.should_receive(:three).ordered
|
28
|
+
@double.one
|
29
|
+
@double.two
|
30
|
+
@double.three
|
31
|
+
@double.rspec_verify
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should fail if second call comes first" do
|
35
|
-
@
|
36
|
-
@
|
35
|
+
@double.should_receive(:one).ordered
|
36
|
+
@double.should_receive(:two).ordered
|
37
37
|
lambda do
|
38
|
-
@
|
39
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, "
|
38
|
+
@double.two
|
39
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :two out of order")
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should fail if third call comes first" do
|
43
|
-
@
|
44
|
-
@
|
45
|
-
@
|
46
|
-
@
|
43
|
+
@double.should_receive(:one).ordered
|
44
|
+
@double.should_receive(:two).ordered
|
45
|
+
@double.should_receive(:three).ordered
|
46
|
+
@double.one
|
47
47
|
lambda do
|
48
|
-
@
|
49
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, "
|
48
|
+
@double.three
|
49
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :three out of order")
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should fail if third call comes second" do
|
53
|
-
@
|
54
|
-
@
|
55
|
-
@
|
56
|
-
@
|
53
|
+
@double.should_receive(:one).ordered
|
54
|
+
@double.should_receive(:two).ordered
|
55
|
+
@double.should_receive(:three).ordered
|
56
|
+
@double.one
|
57
57
|
lambda do
|
58
|
-
@
|
59
|
-
end.should raise_error(Rspec::Mocks::MockExpectationError, "
|
58
|
+
@double.three
|
59
|
+
end.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :three out of order")
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should ignore order of non ordered calls" do
|
63
|
-
@
|
64
|
-
@
|
65
|
-
@
|
66
|
-
@
|
67
|
-
@
|
68
|
-
@
|
69
|
-
@
|
70
|
-
@
|
71
|
-
@
|
72
|
-
@
|
73
|
-
@
|
74
|
-
@
|
75
|
-
@
|
76
|
-
@
|
77
|
-
@
|
78
|
-
@
|
79
|
-
@
|
63
|
+
@double.should_receive(:ignored_0)
|
64
|
+
@double.should_receive(:ordered_1).ordered
|
65
|
+
@double.should_receive(:ignored_1)
|
66
|
+
@double.should_receive(:ordered_2).ordered
|
67
|
+
@double.should_receive(:ignored_2)
|
68
|
+
@double.should_receive(:ignored_3)
|
69
|
+
@double.should_receive(:ordered_3).ordered
|
70
|
+
@double.should_receive(:ignored_4)
|
71
|
+
@double.ignored_3
|
72
|
+
@double.ordered_1
|
73
|
+
@double.ignored_0
|
74
|
+
@double.ordered_2
|
75
|
+
@double.ignored_4
|
76
|
+
@double.ignored_2
|
77
|
+
@double.ordered_3
|
78
|
+
@double.ignored_1
|
79
|
+
@double.rspec_verify
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should pass when duplicates exist" do
|
83
|
-
@
|
84
|
-
@
|
85
|
-
@
|
83
|
+
@double.should_receive(:a).ordered
|
84
|
+
@double.should_receive(:b).ordered
|
85
|
+
@double.should_receive(:a).ordered
|
86
86
|
|
87
|
-
@
|
88
|
-
@
|
89
|
-
@
|
87
|
+
@double.a
|
88
|
+
@double.b
|
89
|
+
@double.a
|
90
90
|
end
|
91
91
|
|
92
92
|
end
|
@@ -31,19 +31,19 @@ module Rspec
|
|
31
31
|
@m2.should be_verified
|
32
32
|
end
|
33
33
|
it "should reset all mocks within" do
|
34
|
-
@space.add(m1 =
|
35
|
-
@space.add(m2 =
|
34
|
+
@space.add(m1 = double("mock1"))
|
35
|
+
@space.add(m2 = double("mock2"))
|
36
36
|
m1.should_receive(:rspec_reset)
|
37
37
|
m2.should_receive(:rspec_reset)
|
38
38
|
@space.reset_all
|
39
39
|
end
|
40
40
|
it "should clear internal mocks on reset_all" do
|
41
|
-
@space.add(m =
|
41
|
+
@space.add(m = double("mock"))
|
42
42
|
@space.reset_all
|
43
43
|
@space.instance_eval { mocks.empty? }.should be_true
|
44
44
|
end
|
45
45
|
it "should only add an instance once" do
|
46
|
-
@space.add(m1 =
|
46
|
+
@space.add(m1 = double("mock1"))
|
47
47
|
@space.add(m1)
|
48
48
|
m1.should_receive(:rspec_verify)
|
49
49
|
@space.verify_all
|
@@ -6,7 +6,7 @@ module Rspec
|
|
6
6
|
treats_method_missing_as_private :subject => Rspec::Mocks::Mock.new, :noop => false
|
7
7
|
|
8
8
|
before(:each) do
|
9
|
-
@mock =
|
9
|
+
@mock = double("test double")
|
10
10
|
end
|
11
11
|
|
12
12
|
after(:each) do
|
@@ -25,7 +25,7 @@ module Rspec
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should report line number of expectation of unreceived message after #should_receive after similar stub" do
|
28
|
-
@mock.stub
|
28
|
+
@mock.stub(:wont_happen)
|
29
29
|
expected_error_line = __LINE__; @mock.should_receive(:wont_happen).with("x", 3)
|
30
30
|
begin
|
31
31
|
@mock.rspec_verify
|
@@ -50,18 +50,24 @@ module Rspec
|
|
50
50
|
|
51
51
|
it "should fail when receiving message specified as not to be received" do
|
52
52
|
@mock.should_not_receive(:not_expected)
|
53
|
-
|
53
|
+
expect {
|
54
54
|
@mock.not_expected
|
55
55
|
violated
|
56
|
-
}.
|
56
|
+
}.to raise_error(
|
57
|
+
Rspec::Mocks::MockExpectationError,
|
58
|
+
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
|
59
|
+
)
|
57
60
|
end
|
58
61
|
|
59
62
|
it "should fail when receiving message specified as not to be received with args" do
|
60
63
|
@mock.should_not_receive(:not_expected).with("unexpected text")
|
61
|
-
|
64
|
+
expect {
|
62
65
|
@mock.not_expected("unexpected text")
|
63
66
|
violated
|
64
|
-
}.
|
67
|
+
}.to raise_error(
|
68
|
+
Rspec::Mocks::MockExpectationError,
|
69
|
+
%Q|(Double "test double").not_expected("unexpected text")\n expected: 0 times\n received: 1 time|
|
70
|
+
)
|
65
71
|
end
|
66
72
|
|
67
73
|
it "should pass when receiving message specified as not to be received with wrong args" do
|
@@ -93,32 +99,32 @@ module Rspec
|
|
93
99
|
lambda {
|
94
100
|
@mock.something("a","d","c")
|
95
101
|
violated
|
96
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
102
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
97
103
|
end
|
98
104
|
|
99
105
|
it "should raise exception if args don't match when method called even when the method is stubbed" do
|
100
|
-
@mock.stub
|
106
|
+
@mock.stub(:something)
|
101
107
|
@mock.should_receive(:something).with("a","b","c")
|
102
108
|
lambda {
|
103
109
|
@mock.something("a","d","c")
|
104
110
|
@mock.rspec_verify
|
105
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
111
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
106
112
|
end
|
107
113
|
|
108
114
|
it "should raise exception if args don't match when method called even when using null_object" do
|
109
|
-
@mock =
|
115
|
+
@mock = double("test double", :null_object => true)
|
110
116
|
@mock.should_receive(:something).with("a","b","c")
|
111
117
|
lambda {
|
112
118
|
@mock.something("a","d","c")
|
113
119
|
@mock.rspec_verify
|
114
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
120
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
115
121
|
end
|
116
122
|
|
117
123
|
it "should fail if unexpected method called" do
|
118
124
|
lambda {
|
119
125
|
@mock.something("a","b","c")
|
120
126
|
violated
|
121
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
127
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received unexpected message :something with (\"a\", \"b\", \"c\")")
|
122
128
|
end
|
123
129
|
|
124
130
|
it "should use block for expectation if provided" do
|
@@ -133,23 +139,25 @@ module Rspec
|
|
133
139
|
|
134
140
|
it "should fail if expectation block fails" do
|
135
141
|
@mock.should_receive(:something) {| bool | bool.should be_true}
|
136
|
-
|
142
|
+
expect {
|
137
143
|
@mock.something false
|
138
|
-
}.
|
144
|
+
}.to raise_error(Rspec::Mocks::MockExpectationError, /Double \"test double\" received :something but passed block failed with: expected false to be true/)
|
139
145
|
end
|
140
146
|
|
141
147
|
it "should fail right away when method defined as never is received" do
|
142
148
|
@mock.should_receive(:not_expected).never
|
143
|
-
|
144
|
-
|
145
|
-
|
149
|
+
expect { @mock.not_expected }.to raise_error(
|
150
|
+
Rspec::Mocks::MockExpectationError,
|
151
|
+
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
|
152
|
+
)
|
146
153
|
end
|
147
154
|
|
148
155
|
it "should eventually fail when method defined as never is received" do
|
149
156
|
@mock.should_receive(:not_expected).never
|
150
|
-
|
151
|
-
|
152
|
-
|
157
|
+
expect { @mock.not_expected }.to raise_error(
|
158
|
+
Rspec::Mocks::MockExpectationError,
|
159
|
+
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
|
160
|
+
)
|
153
161
|
end
|
154
162
|
|
155
163
|
it "should raise when told to" do
|
@@ -209,18 +217,18 @@ module Rspec
|
|
209
217
|
@mock.should_receive(:something).with(no_args())
|
210
218
|
lambda {
|
211
219
|
@mock.something 1
|
212
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
220
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (no args)\n got: (1)")
|
213
221
|
end
|
214
222
|
|
215
223
|
it "should fail when args are expected but none are received" do
|
216
224
|
@mock.should_receive(:something).with(1)
|
217
225
|
lambda {
|
218
226
|
@mock.something
|
219
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
227
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (1)\n got: (no args)")
|
220
228
|
end
|
221
229
|
|
222
230
|
it "should return value from block by default" do
|
223
|
-
@mock.stub
|
231
|
+
@mock.stub(:method_that_yields).and_yield
|
224
232
|
@mock.method_that_yields { :returned_obj }.should == :returned_obj
|
225
233
|
@mock.rspec_verify
|
226
234
|
end
|
@@ -324,7 +332,7 @@ module Rspec
|
|
324
332
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
|
325
333
|
lambda {
|
326
334
|
@mock.yield_back {|a|}
|
327
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
335
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"wha\", \"zup\"| to block with arity of 1")
|
328
336
|
end
|
329
337
|
|
330
338
|
it "should fail when calling yielding method consecutively with wrong arity" do
|
@@ -335,14 +343,14 @@ module Rspec
|
|
335
343
|
a, b = nil
|
336
344
|
c = []
|
337
345
|
@mock.yield_back {|a,b| c << [a, b]}
|
338
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
346
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
|
339
347
|
end
|
340
348
|
|
341
349
|
it "should fail when calling yielding method without block" do
|
342
350
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
|
343
351
|
lambda {
|
344
352
|
@mock.yield_back
|
345
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
353
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, "Double \"test double\" asked to yield |[\"wha\", \"zup\"]| but no block was passed")
|
346
354
|
end
|
347
355
|
|
348
356
|
it "should be able to mock send" do
|
@@ -369,11 +377,11 @@ module Rspec
|
|
369
377
|
@mock.rspec_verify
|
370
378
|
lambda {
|
371
379
|
@mock.foobar
|
372
|
-
}.should raise_error(Rspec::Mocks::MockExpectationError, "
|
380
|
+
}.should raise_error(Rspec::Mocks::MockExpectationError, %q|Double "test double" received unexpected message :foobar with (no args)|)
|
373
381
|
end
|
374
382
|
|
375
383
|
it "should restore objects to their original state on rspec_reset" do
|
376
|
-
mock =
|
384
|
+
mock = double("this is a mock")
|
377
385
|
mock.should_receive(:blah)
|
378
386
|
mock.rspec_reset
|
379
387
|
mock.rspec_verify #should throw if reset didn't work
|
@@ -409,7 +417,7 @@ module Rspec
|
|
409
417
|
end
|
410
418
|
|
411
419
|
it "should temporarily replace a method stub on a mock" do
|
412
|
-
@mock.stub
|
420
|
+
@mock.stub(:msg).and_return(:stub_value)
|
413
421
|
@mock.should_receive(:msg).with(:arg).and_return(:mock_value)
|
414
422
|
@mock.msg(:arg).should equal(:mock_value)
|
415
423
|
@mock.msg.should equal(:stub_value)
|
@@ -418,7 +426,7 @@ module Rspec
|
|
418
426
|
end
|
419
427
|
|
420
428
|
it "should not require a different signature to replace a method stub" do
|
421
|
-
@mock.stub
|
429
|
+
@mock.stub(:msg).and_return(:stub_value)
|
422
430
|
@mock.should_receive(:msg).and_return(:mock_value)
|
423
431
|
@mock.msg(:arg).should equal(:mock_value)
|
424
432
|
@mock.msg.should equal(:stub_value)
|
@@ -427,14 +435,14 @@ module Rspec
|
|
427
435
|
end
|
428
436
|
|
429
437
|
it "should raise an error when a previously stubbed method has a negative expectation" do
|
430
|
-
@mock.stub
|
438
|
+
@mock.stub(:msg).and_return(:stub_value)
|
431
439
|
@mock.should_not_receive(:msg).and_return(:mock_value)
|
432
440
|
lambda {@mock.msg(:arg)}.should raise_error(Rspec::Mocks::MockExpectationError)
|
433
441
|
end
|
434
442
|
|
435
443
|
it "should temporarily replace a method stub on a non-mock" do
|
436
444
|
non_mock = Object.new
|
437
|
-
non_mock.stub
|
445
|
+
non_mock.stub(:msg).and_return(:stub_value)
|
438
446
|
non_mock.should_receive(:msg).with(:arg).and_return(:mock_value)
|
439
447
|
non_mock.msg(:arg).should equal(:mock_value)
|
440
448
|
non_mock.msg.should equal(:stub_value)
|
@@ -443,14 +451,14 @@ module Rspec
|
|
443
451
|
end
|
444
452
|
|
445
453
|
it "should return the stubbed value when no new value specified" do
|
446
|
-
@mock.stub
|
454
|
+
@mock.stub(:msg).and_return(:stub_value)
|
447
455
|
@mock.should_receive(:msg)
|
448
456
|
@mock.msg.should equal(:stub_value)
|
449
457
|
@mock.rspec_verify
|
450
458
|
end
|
451
459
|
|
452
460
|
it "should not mess with the stub's yielded values when also mocked" do
|
453
|
-
@mock.stub
|
461
|
+
@mock.stub(:yield_back).and_yield(:stub_value)
|
454
462
|
@mock.should_receive(:yield_back).and_yield(:mock_value)
|
455
463
|
@mock.yield_back{|v| v.should == :mock_value }
|
456
464
|
@mock.yield_back{|v| v.should == :stub_value }
|
@@ -458,7 +466,7 @@ module Rspec
|
|
458
466
|
end
|
459
467
|
|
460
468
|
it "should yield multiple values after a similar stub" do
|
461
|
-
File.stub
|
469
|
+
File.stub(:open).and_yield(:stub_value)
|
462
470
|
File.should_receive(:open).and_yield(:first_call).and_yield(:second_call)
|
463
471
|
yielded_args = []
|
464
472
|
File.open {|v| yielded_args << v }
|
@@ -476,7 +484,7 @@ module Rspec
|
|
476
484
|
|
477
485
|
describe "a mock message receiving a block" do
|
478
486
|
before(:each) do
|
479
|
-
@mock =
|
487
|
+
@mock = double("mock")
|
480
488
|
@calls = 0
|
481
489
|
end
|
482
490
|
|
@@ -493,7 +501,7 @@ module Rspec
|
|
493
501
|
end
|
494
502
|
|
495
503
|
it "should call the block after #should_receive after a similar stub" do
|
496
|
-
@mock.stub
|
504
|
+
@mock.stub(:foo).and_return(:bar)
|
497
505
|
@mock.should_receive(:foo) { add_call }
|
498
506
|
|
499
507
|
@mock.foo
|
@@ -547,33 +555,29 @@ module Rspec
|
|
547
555
|
|
548
556
|
describe 'string representation generated by #to_s' do
|
549
557
|
it 'should not contain < because that might lead to invalid HTML in some situations' do
|
550
|
-
mock =
|
558
|
+
mock = double("Dog")
|
551
559
|
valid_html_str = "#{mock}"
|
552
560
|
valid_html_str.should_not include('<')
|
553
561
|
end
|
554
562
|
end
|
555
563
|
|
556
564
|
describe "mock created with no name" do
|
557
|
-
it "should name
|
558
|
-
mock
|
559
|
-
|
560
|
-
|
561
|
-
it "should name itself after initially stubbed methods" do
|
562
|
-
string = mock(:foo => "woo", :bar => "car").to_s
|
563
|
-
string.should include('foo: \"woo\"', 'bar: \"car\"')
|
565
|
+
it "should not use a name in a failure message" do
|
566
|
+
mock = double()
|
567
|
+
expect {mock.foo}.to raise_error(/Double received/)
|
564
568
|
end
|
565
|
-
|
569
|
+
|
566
570
|
it "should respond to initially stubbed methods" do
|
567
|
-
|
568
|
-
|
569
|
-
|
571
|
+
double = double(:foo => "woo", :bar => "car")
|
572
|
+
double.foo.should == "woo"
|
573
|
+
double.bar.should == "car"
|
570
574
|
end
|
571
575
|
end
|
572
576
|
|
573
577
|
describe "==" do
|
574
578
|
it "sends '== self' to the comparison object" do
|
575
|
-
first =
|
576
|
-
second =
|
579
|
+
first = double('first')
|
580
|
+
second = double('second')
|
577
581
|
|
578
582
|
first.should_receive(:==).with(second)
|
579
583
|
second == first
|