/
test_doubles_and_stubs.feature
101 lines (88 loc) · 2.73 KB
/
test_doubles_and_stubs.feature
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
Feature: Test doubles and stubs
Use test doubles and stubs everywhere! They help decoupling :)
Scenario: declare a test double
Given a directory named "stendhal_project"
When I cd to "stendhal_project"
Given a file named "sample_spec.rb" with:
"""
class MyClass
def initialize(logger)
@logger = logger
end
end
describe "something" do
it "declares test doubles with :fake helper" do
logger = fake('logger')
object = MyClass.new(logger)
object.must be_a(MyClass)
end
it "declares test doubles with :double helper as well" do
logger = double('logger')
object = MyClass.new(logger)
object.must be_a(MyClass)
end
end
"""
When I run "stendhal sample_spec.rb"
Then the exit status should be 0
And the output should contain "2 examples, 0 failures"
Scenario: declare a test double with stubs
Given a directory named "stendhal_project"
When I cd to "stendhal_project"
Given a file named "sample_spec.rb" with:
"""
class MyClass
def initialize(logger)
@logger = logger
end
def do_something
@logger.log "Logger"
if @logger.print
return "result"
end
end
end
describe "something" do
it "does something" do
logger = fake('logger', :log => nil, :print => true)
object = MyClass.new(logger)
object.do_something.must eq("result")
end
end
"""
When I run "stendhal sample_spec.rb"
Then the exit status should be 0
And the output should contain "1 example, 0 failures"
Scenario: partial stubbing
Given a directory named "stendhal_project"
When I cd to "stendhal_project"
Given a file named "sample_spec.rb" with:
"""
class MyClass
def initialize
end
def do_something
"some result"
end
end
describe "something" do
it "stubs an existing method" do
object = MyClass.new
object.stubs(:do_something)
object.do_something.must be_nil
end
it "stubs an existing method with a return value" do
object = MyClass.new
object.stubs(:do_something) { "another result" }
object.do_something.must eq('another result')
end
it "stubs an unexisting method" do
object = MyClass.new
object.stubs(:invented_method) { "invented result" }
object.invented_method.must eq("invented result")
end
end
"""
When I run "stendhal sample_spec.rb"
Then the exit status should be 0
And the output should contain "3 examples, 0 failures"