stendhal 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stendhal (0.1.6)
4
+ stendhal (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/Readme.md CHANGED
@@ -11,7 +11,7 @@ development.
11
11
  ##Current features
12
12
 
13
13
  * Pretty decent reporter with colors
14
- * Test doubles and stubs (no partial stubbing yet)
14
+ * Test doubles and stubs (also partial stubbing!)
15
15
  * Mocks (message expectations) with _optionally_ stubbable return values
16
16
  * Nested example groups (declare them with either describe or context)
17
17
  * Pending examples
@@ -88,6 +88,22 @@ development.
88
88
  end
89
89
  end
90
90
 
91
+ describe "Partial stubbing" do
92
+ it "returns nil by default" do
93
+ string = "my string"
94
+ string.stubs(:some_method)
95
+
96
+ string.some_method # => nil
97
+ end
98
+
99
+ it "returns a value if you tell it to" do
100
+ string = "my string"
101
+ string.stubs(:some_method) { 'some value' }
102
+
103
+ string.some_method # => "some value"
104
+ end
105
+ end
106
+
91
107
  describe "Message expectation" do
92
108
  it "is declared with expects" do
93
109
  string = "my string"
@@ -156,6 +172,10 @@ development.
156
172
  * is declared with fake
157
173
  * is declared with double as well
158
174
  * can be given stubs
175
+
176
+ Partial stubbing
177
+ * returns nil by default
178
+ * returns a value if you tell it to
159
179
 
160
180
  Message expectation
161
181
  * is declared with expects
@@ -164,7 +184,7 @@ development.
164
184
  * can return a stubbed proc
165
185
  * is declared with does_not_expect in case it is negative [FAILED]
166
186
 
167
- 16 examples, 4 failures, 2 pending
187
+ 18 examples, 4 failures, 2 pending
168
188
 
169
189
  ##Feedback
170
190
 
@@ -61,3 +61,41 @@ Feature: Test doubles and stubs
61
61
  When I run "stendhal sample_spec.rb"
62
62
  Then the exit status should be 0
63
63
  And the output should contain "1 example, 0 failures"
64
+
65
+ Scenario: partial stubbing
66
+ Given a directory named "stendhal_project"
67
+ When I cd to "stendhal_project"
68
+ Given a file named "sample_spec.rb" with:
69
+ """
70
+ class MyClass
71
+ def initialize
72
+ end
73
+ def do_something
74
+ "some result"
75
+ end
76
+ end
77
+
78
+ describe "something" do
79
+ it "stubs an existing method" do
80
+ object = MyClass.new
81
+
82
+ object.stubs(:do_something)
83
+ object.do_something.must be_nil
84
+ end
85
+ it "stubs an existing method with a return value" do
86
+ object = MyClass.new
87
+
88
+ object.stubs(:do_something) { "another result" }
89
+ object.do_something.must eq('another result')
90
+ end
91
+ it "stubs an unexisting method" do
92
+ object = MyClass.new
93
+
94
+ object.stubs(:invented_method) { "invented result" }
95
+ object.invented_method.must eq("invented result")
96
+ end
97
+ end
98
+ """
99
+ When I run "stendhal sample_spec.rb"
100
+ Then the exit status should be 0
101
+ And the output should contain "3 examples, 0 failures"
@@ -1,3 +1,4 @@
1
1
  class Object
2
2
  include Stendhal::Mocks::Mockable
3
+ include Stendhal::Mocks::Stubbable
3
4
  end
@@ -1,5 +1,6 @@
1
1
  require 'stendhal/mocks/test_double'
2
2
  require 'stendhal/mocks/mock_verifier'
3
+ require 'stendhal/mocks/stubbable'
3
4
  require 'stendhal/mocks/mockable'
4
5
 
5
6
  module Stendhal
@@ -48,7 +48,6 @@ EOT
48
48
  def and_returns(retval = nil, &block)
49
49
  raise "This object has no mocks." unless @__verifier
50
50
  method = __verifier.last_mocked_method
51
- str = retval.to_s
52
51
  unless respond_to?(:"__unstubbed_#{method}")
53
52
  metaclass = (class << self;self;end)
54
53
  metaclass.send(:alias_method, :"__unstubbed_#{method}", :"__original_#{method}")
@@ -0,0 +1,22 @@
1
+ module Stendhal
2
+ module Mocks
3
+ module Stubbable
4
+
5
+ def stubs(method, &block)
6
+ unless respond_to?(:"__unstubbed_#{method}")
7
+ metaclass = (class << self;self;end)
8
+
9
+ if respond_to? method.to_sym
10
+ metaclass.send(:alias_method, :"__unstubbed_#{method}", method.to_sym)
11
+ metaclass.send(:undef_method, method.to_sym)
12
+ end
13
+
14
+ return_value = block || Proc.new { nil }
15
+ metaclass.send(:define_method, method.to_sym, return_value)
16
+ end
17
+ self
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module Stendhal
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ class MyArray < Array
4
+ include Stendhal::Mocks::Stubbable
5
+ end
6
+
7
+ module Stendhal
8
+ module Mocks
9
+ describe "a Stubbable class" do
10
+
11
+ subject { MyArray.new([1,2,3]) }
12
+
13
+ describe "#stubs" do
14
+ it 'stubs an existing method and makes it return nil' do
15
+ subject.stubs(:length)
16
+ subject.length.should be_nil
17
+ end
18
+ it 'stubs an unexisting method and makes it return nil' do
19
+ subject.stubs(:w00t)
20
+ subject.w00t.should be_nil
21
+ end
22
+ it 'saves the unstubbed method' do
23
+ subject.stubs(:length)
24
+ subject.should respond_to(:__unstubbed_length)
25
+ subject.send(:__unstubbed_length).should == 3
26
+ end
27
+ context "with a block" do
28
+ it 'makes the stub return the result of the block' do
29
+ subject.stubs(:w00t) { 3 + 4 }
30
+ subject.w00t.should eq(7)
31
+ end
32
+ end
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
- - 6
9
- version: 0.1.6
8
+ - 7
9
+ version: 0.1.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josep M. Bach
@@ -151,6 +151,7 @@ files:
151
151
  - lib/stendhal/mocks.rb
152
152
  - lib/stendhal/mocks/mock_verifier.rb
153
153
  - lib/stendhal/mocks/mockable.rb
154
+ - lib/stendhal/mocks/stubbable.rb
154
155
  - lib/stendhal/mocks/test_double.rb
155
156
  - lib/stendhal/reporter.rb
156
157
  - lib/stendhal/version.rb
@@ -166,6 +167,7 @@ files:
166
167
  - spec/stendhal/matchers_spec.rb
167
168
  - spec/stendhal/mocks/mock_verifier_spec.rb
168
169
  - spec/stendhal/mocks/mockable_spec.rb
170
+ - spec/stendhal/mocks/stubbable_spec.rb
169
171
  - spec/stendhal/mocks/test_double_spec.rb
170
172
  - spec/stendhal/reporter_spec.rb
171
173
  - stendhal.gemspec
@@ -219,5 +221,6 @@ test_files:
219
221
  - spec/stendhal/matchers_spec.rb
220
222
  - spec/stendhal/mocks/mock_verifier_spec.rb
221
223
  - spec/stendhal/mocks/mockable_spec.rb
224
+ - spec/stendhal/mocks/stubbable_spec.rb
222
225
  - spec/stendhal/mocks/test_double_spec.rb
223
226
  - spec/stendhal/reporter_spec.rb