stendhal 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/Readme.md +25 -1
- data/features/test_doubles_and_stubs.feature +63 -0
- data/lib/stendhal/example.rb +3 -0
- data/lib/stendhal/example_group.rb +6 -3
- data/lib/stendhal/matchers.rb +6 -0
- data/lib/stendhal/mocks/test_double.rb +23 -0
- data/lib/stendhal/mocks.rb +10 -0
- data/lib/stendhal/version.rb +1 -1
- data/lib/stendhal.rb +1 -0
- data/spec/stendhal/example_spec.rb +9 -0
- data/spec/stendhal/matchers_spec.rb +4 -3
- data/spec/stendhal/test_double_spec.rb +37 -0
- metadata +9 -3
data/Gemfile.lock
CHANGED
data/Readme.md
CHANGED
@@ -8,12 +8,16 @@ development.
|
|
8
8
|
|
9
9
|
##Current features
|
10
10
|
|
11
|
+
* Test doubles and stubs (no partial stubbing yet)
|
11
12
|
* Nested example groups
|
12
13
|
* Pending examples
|
13
14
|
* Lame reporter (but will get better eventually)
|
14
15
|
* Matchers (use with object.must or object.must_not)
|
16
|
+
|
15
17
|
eq() / eql()
|
18
|
+
|
16
19
|
be_a() / be_kind_of() / be_a_kind_of()
|
20
|
+
|
17
21
|
be_whatever # asks object.whatever?
|
18
22
|
|
19
23
|
|
@@ -60,6 +64,21 @@ development.
|
|
60
64
|
|
61
65
|
end
|
62
66
|
|
67
|
+
describe "Test double" do
|
68
|
+
it "is declared with fake" do
|
69
|
+
my_logger = fake('logger')
|
70
|
+
end
|
71
|
+
|
72
|
+
it "is declared with double as well" do
|
73
|
+
my_logger = double('logger')
|
74
|
+
end
|
75
|
+
|
76
|
+
it "can be given stubs" do
|
77
|
+
my_logger = double('logger', :my_method => 6)
|
78
|
+
my_logger.my_method.must eq(6)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
63
82
|
end
|
64
83
|
|
65
84
|
###Running the specs!
|
@@ -81,7 +100,12 @@ development.
|
|
81
100
|
* should do something but I don't know what yet
|
82
101
|
* will do something else
|
83
102
|
|
84
|
-
|
103
|
+
Test double
|
104
|
+
* is declared with fake
|
105
|
+
* is declared with double as well
|
106
|
+
* can be given stubs
|
107
|
+
|
108
|
+
10 examples, 2 failures, 2 pending
|
85
109
|
|
86
110
|
##Note on Patches/Pull Requests
|
87
111
|
|
@@ -0,0 +1,63 @@
|
|
1
|
+
Feature: Test doubles and stubs
|
2
|
+
|
3
|
+
Use test doubles and stubs everywhere! They help decoupling :)
|
4
|
+
|
5
|
+
Scenario: declare a test double
|
6
|
+
Given a directory named "stendhal_project"
|
7
|
+
When I cd to "stendhal_project"
|
8
|
+
Given a file named "sample_spec.rb" with:
|
9
|
+
"""
|
10
|
+
class MyClass
|
11
|
+
def initialize(logger)
|
12
|
+
@logger = logger
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "something" do
|
17
|
+
it "declares test doubles with :fake helper" do
|
18
|
+
logger = fake('logger')
|
19
|
+
object = MyClass.new(logger)
|
20
|
+
|
21
|
+
object.must be_a(MyClass)
|
22
|
+
end
|
23
|
+
it "declares test doubles with :double helper as well" do
|
24
|
+
logger = double('logger')
|
25
|
+
object = MyClass.new(logger)
|
26
|
+
|
27
|
+
object.must be_a(MyClass)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
"""
|
31
|
+
When I run "stendhal sample_spec.rb"
|
32
|
+
Then the exit status should be 0
|
33
|
+
And the output should contain "2 examples, 0 failures"
|
34
|
+
|
35
|
+
Scenario: declare a test double with stubs
|
36
|
+
Given a directory named "stendhal_project"
|
37
|
+
When I cd to "stendhal_project"
|
38
|
+
Given a file named "sample_spec.rb" with:
|
39
|
+
"""
|
40
|
+
class MyClass
|
41
|
+
def initialize(logger)
|
42
|
+
@logger = logger
|
43
|
+
end
|
44
|
+
def do_something
|
45
|
+
@logger.log "Logger"
|
46
|
+
if @logger.print
|
47
|
+
return "result"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "something" do
|
53
|
+
it "does something" do
|
54
|
+
logger = fake('logger', :log => nil, :print => true)
|
55
|
+
object = MyClass.new(logger)
|
56
|
+
|
57
|
+
object.do_something.must eq("result")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
"""
|
61
|
+
When I run "stendhal sample_spec.rb"
|
62
|
+
Then the exit status should be 0
|
63
|
+
And the output should contain "1 example, 0 failures"
|
data/lib/stendhal/example.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Stendhal
|
2
2
|
class Example
|
3
3
|
include Matchers
|
4
|
+
include Mocks
|
4
5
|
|
5
6
|
@@examples = []
|
6
7
|
|
7
8
|
attr_reader :description
|
8
9
|
attr_reader :block
|
9
10
|
attr_reader :failed_message
|
11
|
+
attr_reader :aborted_message
|
10
12
|
|
11
13
|
def initialize(docstring, options = {}, &block)
|
12
14
|
@description = docstring
|
@@ -25,6 +27,7 @@ module Stendhal
|
|
25
27
|
return 1
|
26
28
|
rescue StandardError=>e
|
27
29
|
@aborted = true
|
30
|
+
@aborted_message = e.message
|
28
31
|
return 1
|
29
32
|
end
|
30
33
|
end
|
@@ -19,15 +19,18 @@ module Stendhal
|
|
19
19
|
examples.reject do |example|
|
20
20
|
if example.pending?
|
21
21
|
pending += 1
|
22
|
-
$stdout.print "* #{example.description}\n"
|
22
|
+
$stdout.print "* #{example.description} [PENDING]\n"
|
23
23
|
true
|
24
24
|
else
|
25
25
|
false
|
26
26
|
end
|
27
27
|
end.each do |example|
|
28
28
|
failures += example.run
|
29
|
-
|
30
|
-
|
29
|
+
status = " [FAILED]" if example.failed?
|
30
|
+
status = " [ABORTED]" if example.aborted?
|
31
|
+
$stdout.print "* #{example.description}#{status || ''}\n"
|
32
|
+
$stdout.print "\t#{example.failed_message}\n" if example.failed?
|
33
|
+
$stdout.print "\t#{example.aborted_message}\n" if example.aborted?
|
31
34
|
end
|
32
35
|
[examples.count, failures, pending]
|
33
36
|
end
|
data/lib/stendhal/matchers.rb
CHANGED
@@ -10,6 +10,10 @@ module Stendhal
|
|
10
10
|
end
|
11
11
|
alias_method :eql, :eq
|
12
12
|
|
13
|
+
def caca(other)
|
14
|
+
Equality.new(other)
|
15
|
+
end
|
16
|
+
|
13
17
|
def be_a(kind)
|
14
18
|
Kind.new(kind)
|
15
19
|
end
|
@@ -19,6 +23,8 @@ module Stendhal
|
|
19
23
|
def method_missing(m,*args)
|
20
24
|
if m.to_s =~ /be_(\w+)/
|
21
25
|
Predicate.new(($1 + '?').to_sym)
|
26
|
+
else
|
27
|
+
super(m,*args)
|
22
28
|
end
|
23
29
|
end
|
24
30
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Stendhal
|
2
|
+
module Mocks
|
3
|
+
class TestDouble
|
4
|
+
|
5
|
+
attr_accessor :name
|
6
|
+
|
7
|
+
def initialize(name, stubs = {})
|
8
|
+
@name = name
|
9
|
+
assign_stubs(stubs)
|
10
|
+
end
|
11
|
+
|
12
|
+
def assign_stubs(stubs)
|
13
|
+
stubs.each_pair do |k,v|
|
14
|
+
(class << self;self;end).send(:define_method,k.to_sym) do |*args|
|
15
|
+
v
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/stendhal/version.rb
CHANGED
data/lib/stendhal.rb
CHANGED
@@ -51,6 +51,15 @@ module Stendhal
|
|
51
51
|
example.should be_aborted
|
52
52
|
end
|
53
53
|
|
54
|
+
it "captures everything else" do
|
55
|
+
example = Example.new("docstring") do
|
56
|
+
hello my dear reader
|
57
|
+
end
|
58
|
+
expect {example.run}.to_not raise_error
|
59
|
+
example.run
|
60
|
+
example.should be_aborted
|
61
|
+
end
|
62
|
+
|
54
63
|
end
|
55
64
|
|
56
65
|
describe "#fail" do
|
@@ -9,10 +9,11 @@ module Stendhal
|
|
9
9
|
|
10
10
|
let(:object) { MyClass.new }
|
11
11
|
|
12
|
-
describe "
|
12
|
+
describe "#eql / #eq" do
|
13
13
|
it 'creates a new equality expectation' do
|
14
|
-
Matchers::Equality.should_receive(:new).with(7)
|
15
|
-
object
|
14
|
+
Matchers::Equality.should_receive(:new).with(7).twice
|
15
|
+
object.eq(7)
|
16
|
+
object.eql(7)
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Stendhal
|
4
|
+
module Mocks
|
5
|
+
describe TestDouble do
|
6
|
+
|
7
|
+
subject { TestDouble.new('double') }
|
8
|
+
|
9
|
+
it 'is created with a name' do
|
10
|
+
subject.name.should == 'double'
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when given a hash of stubs" do
|
14
|
+
|
15
|
+
subject { TestDouble.new('double', :my_stub => 3) }
|
16
|
+
|
17
|
+
it 'assigns them to the test double' do
|
18
|
+
subject.should respond_to(:my_stub)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'makes every stub return its value' do
|
22
|
+
subject.my_stub.should == 3
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when giving arguments to the subbed method" do
|
26
|
+
|
27
|
+
it 'ignores them' do
|
28
|
+
subject.my_stub("argument").should == 3
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Josep M. Bach
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-11-03 00:00:00 +01:00
|
18
18
|
default_executable: stendhal
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- features/examples.feature
|
93
93
|
- features/expectations.feature
|
94
94
|
- features/support/env.rb
|
95
|
+
- features/test_doubles_and_stubs.feature
|
95
96
|
- lib/stendhal.rb
|
96
97
|
- lib/stendhal/autorun.rb
|
97
98
|
- lib/stendhal/core_ext/kernel.rb
|
@@ -104,6 +105,8 @@ files:
|
|
104
105
|
- lib/stendhal/matchers/equality.rb
|
105
106
|
- lib/stendhal/matchers/kind.rb
|
106
107
|
- lib/stendhal/matchers/predicate.rb
|
108
|
+
- lib/stendhal/mocks.rb
|
109
|
+
- lib/stendhal/mocks/test_double.rb
|
107
110
|
- lib/stendhal/version.rb
|
108
111
|
- script/console
|
109
112
|
- spec/spec_helper.rb
|
@@ -115,6 +118,7 @@ files:
|
|
115
118
|
- spec/stendhal/matchers/kind_spec.rb
|
116
119
|
- spec/stendhal/matchers/predicate_spec.rb
|
117
120
|
- spec/stendhal/matchers_spec.rb
|
121
|
+
- spec/stendhal/test_double_spec.rb
|
118
122
|
- stendhal.gemspec
|
119
123
|
has_rdoc: true
|
120
124
|
homepage: http://rubygems.org/gems/stendhal
|
@@ -152,6 +156,7 @@ test_files:
|
|
152
156
|
- features/examples.feature
|
153
157
|
- features/expectations.feature
|
154
158
|
- features/support/env.rb
|
159
|
+
- features/test_doubles_and_stubs.feature
|
155
160
|
- spec/spec_helper.rb
|
156
161
|
- spec/stendhal/core_ext/kernel_spec.rb
|
157
162
|
- spec/stendhal/example_group_spec.rb
|
@@ -161,3 +166,4 @@ test_files:
|
|
161
166
|
- spec/stendhal/matchers/kind_spec.rb
|
162
167
|
- spec/stendhal/matchers/predicate_spec.rb
|
163
168
|
- spec/stendhal/matchers_spec.rb
|
169
|
+
- spec/stendhal/test_double_spec.rb
|