1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
5 describe "A method stub" do
8 def self.existing_class_method
12 def existing_instance_method
19 it "should allow for a mock expectation to temporarily replace a method stub on a mock" do
20 mock = Spec::Mocks::Mock.new("a mock")
21 mock.stub!(:msg).and_return(:stub_value)
22 mock.should_receive(:msg).with(:arg).and_return(:mock_value)
23 mock.msg(:arg).should equal(:mock_value)
24 mock.msg.should equal(:stub_value)
25 mock.msg.should equal(:stub_value)
29 it "should allow for a mock expectation to temporarily replace a method stub on a non-mock" do
30 @obj.stub!(:msg).and_return(:stub_value)
31 @obj.should_receive(:msg).with(:arg).and_return(:mock_value)
32 @obj.msg(:arg).should equal(:mock_value)
33 @obj.msg.should equal(:stub_value)
34 @obj.msg.should equal(:stub_value)
38 it "should ignore when expected message is not received" do
42 end.should_not raise_error
45 it "should clear itself on rspec_verify" do
46 @obj.stub!(:this_should_go).and_return(:blah)
47 @obj.this_should_go.should == :blah
51 end.should raise_error
54 it "should ignore when expected message is received" do
60 it "should ignore when message is received with args" do
66 it "should return expected value when expected message is received" do
67 @obj.stub!(:msg).and_return(:return_value)
68 @obj.msg.should equal(:return_value)
72 it "should return values in order to consecutive calls" do
73 return_values = ["1",2,Object.new]
74 @obj.stub!(:msg).and_return(return_values[0],return_values[1],return_values[2])
75 @obj.msg.should == return_values[0]
76 @obj.msg.should == return_values[1]
77 @obj.msg.should == return_values[2]
80 it "should keep returning last value in consecutive calls" do
81 return_values = ["1",2,Object.new]
82 @obj.stub!(:msg).and_return(return_values[0],return_values[1],return_values[2])
83 @obj.msg.should == return_values[0]
84 @obj.msg.should == return_values[1]
85 @obj.msg.should == return_values[2]
86 @obj.msg.should == return_values[2]
87 @obj.msg.should == return_values[2]
90 it "should revert to original instance method if existed" do
91 @obj.existing_instance_method.should equal(:original_value)
92 @obj.stub!(:existing_instance_method).and_return(:mock_value)
93 @obj.existing_instance_method.should equal(:mock_value)
95 @obj.existing_instance_method.should equal(:original_value)
98 it "should revert to original class method if existed" do
99 @class.existing_class_method.should equal(:original_value)
100 @class.stub!(:existing_class_method).and_return(:mock_value)
101 @class.existing_class_method.should equal(:mock_value)
103 @class.existing_class_method.should equal(:original_value)
106 it "should support yielding" do
107 @obj.stub!(:method_that_yields).and_yield(:yielded_value)
108 current_value = :value_before
109 @obj.method_that_yields {|val| current_value = val}
110 current_value.should == :yielded_value
114 it "should support yielding multiple times" do
115 @obj.stub!(:method_that_yields_multiple_times).and_yield(:yielded_value).
116 and_yield(:another_value)
118 @obj.method_that_yields_multiple_times {|val| current_value << val}
119 current_value.should == [:yielded_value, :another_value]
123 it "should throw when told to" do
124 @mock.stub!(:something).and_throw(:blech)
127 end.should throw_symbol(:blech)
130 it "should support overriding w/ a new stub" do
131 @stub.stub!(:existing_instance_method).and_return(:updated_stub_value)
132 @stub.existing_instance_method.should == :updated_stub_value
135 it "should support stub with" do
136 @stub.stub!(:foo).with("bar")
137 @stub.should_receive(:foo).with("baz")
143 describe "A method stub with args" do
146 @stub.stub!(:foo).with("bar")
149 it "should not complain if not called" do
152 it "should not complain if called with arg" do
156 it "should complain if called with no arg" do
159 end.should raise_error
162 it "should complain if called with other arg" do
165 end.should raise_error
168 it "should not complain if also mocked w/ different args" do
169 @stub.should_receive(:foo).with("baz")
174 it "should complain if also mocked w/ different args AND called w/ a 3rd set of args" do
175 @stub.should_receive(:foo).with("baz")
180 end.should raise_error
183 it "should support options" do
184 @stub.stub!(:foo, :expected_from => "bar")