This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
stubbing_example.rb
69 lines (58 loc) · 2.18 KB
/
stubbing_example.rb
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
require File.dirname(__FILE__) + '/spec_helper'
describe "A consumer of a stub" do
it "should be able to stub methods on any Object" do
obj = Object.new
obj.stub!(:foobar).and_return {:return_value}
obj.foobar.should equal(:return_value)
end
end
class StubbableClass
def self.find id
return :original_return
end
end
describe "A stubbed method on a class" do
it "should return the stubbed value" do
StubbableClass.stub!(:find).and_return(:stub_return)
StubbableClass.find(1).should equal(:stub_return)
end
it "should revert to the original method after each spec" do
StubbableClass.find(1).should equal(:original_return)
end
it "can stub! and mock the same message" do
StubbableClass.stub!(:msg).and_return(:stub_value)
StubbableClass.should_receive(:msg).with(:arg).and_return(:mock_value)
StubbableClass.msg.should equal(:stub_value)
StubbableClass.msg(:other_arg).should equal(:stub_value)
StubbableClass.msg(:arg).should equal(:mock_value)
StubbableClass.msg(:another_arg).should equal(:stub_value)
StubbableClass.msg(:yet_another_arg).should equal(:stub_value)
StubbableClass.msg.should equal(:stub_value)
end
end
describe "A mock" do
it "can stub!" do
mock = mock("stubbing mock")
mock.stub!(:msg).and_return(:value)
(1..10).each {mock.msg.should equal(:value)}
end
it "can stub! and mock" do
mock = mock("stubbing mock")
mock.stub!(:stub_message).and_return(:stub_value)
mock.should_receive(:mock_message).once.and_return(:mock_value)
(1..10).each {mock.stub_message.should equal(:stub_value)}
mock.mock_message.should equal(:mock_value)
(1..10).each {mock.stub_message.should equal(:stub_value)}
end
it "can stub! and mock the same message" do
mock = mock("stubbing mock")
mock.stub!(:msg).and_return(:stub_value)
mock.should_receive(:msg).with(:arg).and_return(:mock_value)
mock.msg.should equal(:stub_value)
mock.msg(:other_arg).should equal(:stub_value)
mock.msg(:arg).should equal(:mock_value)
mock.msg(:another_arg).should equal(:stub_value)
mock.msg(:yet_another_arg).should equal(:stub_value)
mock.msg.should equal(:stub_value)
end
end