1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
require File.dirname(__FILE__) + '/../../spec_helper.rb'
module Spec
module Mocks
describe "using a Partial Mock," do
before(:each) do
@object = Object.new
end
it "should name the class in the failure message" do
@object.should_receive(:foo)
lambda do
@object.rspec_verify
end.should raise_error(Spec::Mocks::MockExpectationError, /Object/)
end
it "should not conflict with @options in the object" do
@object.instance_eval { @options = Object.new }
@object.should_receive(:blah)
@object.blah
end
it "should_not_receive should mock out the method" do
@object.should_not_receive(:fuhbar)
@object.fuhbar
lambda do
@object.rspec_verify
end.should raise_error(Spec::Mocks::MockExpectationError)
end
it "should_not_receive should return a negative message expectation" do
@object.should_not_receive(:foobar).should be_kind_of(NegativeMessageExpectation)
end
it "should_receive should mock out the method" do
@object.should_receive(:foobar).with(:test_param).and_return(1)
@object.foobar(:test_param).should equal(1)
end
it "should_receive should handle a hash" do
@object.should_receive(:foobar).with(:key => "value").and_return(1)
@object.foobar(:key => "value").should equal(1)
end
it "should_receive should handle an inner hash" do
hash = {:a => {:key => "value"}}
@object.should_receive(:foobar).with(:key => "value").and_return(1)
@object.foobar(hash[:a]).should equal(1)
end
it "should_receive should return a message expectation" do
@object.should_receive(:foobar).should be_kind_of(MessageExpectation)
@object.foobar
end
it "should_receive should verify method was called" do
@object.should_receive(:foobar).with(:test_param).and_return(1)
lambda do
@object.rspec_verify
end.should raise_error(Spec::Mocks::MockExpectationError)
end
it "should_receive should also take a String argument" do
@object.should_receive('foobar')
@object.foobar
end
it "should_not_receive should also take a String argument" do
@object.should_not_receive('foobar')
@object.foobar
lambda do
@object.rspec_verify
end.should raise_error(Spec::Mocks::MockExpectationError)
end
it "should use report nil in the error message" do
@this_will_resolve_to_nil.should_receive(:foobar)
lambda do
@this_will_resolve_to_nil.rspec_verify
end.should raise_error(Spec::Mocks::MockExpectationError, /NilClass.*expected :foobar with/)
end
end
describe "Partially mocking an object that defines ==, after another mock has been defined" do
before(:each) do
stub("existing mock", :foo => :foo)
end
class PartiallyMockedEquals
attr_reader :val
def initialize(val)
@val = val
end
def ==(other)
@val == other.val
end
end
it "should not raise an error when stubbing the object" do
o = PartiallyMockedEquals.new :foo
lambda { o.stub!(:bar) }.should_not raise_error(NoMethodError)
end
end
end
end
|