Permalink
Browse files

0.1.7 - Partial stubbing

  • Loading branch information...
1 parent 79b9f38 commit 4d43184d2a65a2d7f733ed0e3a61a7b50c825da3 @txus committed Nov 13, 2010
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- stendhal (0.1.6)
+ stendhal (0.1.7)
GEM
remote: http://rubygems.org/
View
@@ -11,7 +11,7 @@ development.
##Current features
* Pretty decent reporter with colors
-* Test doubles and stubs (no partial stubbing yet)
+* Test doubles and stubs (also partial stubbing!)
* Mocks (message expectations) with _optionally_ stubbable return values
* Nested example groups (declare them with either describe or context)
* Pending examples
@@ -88,6 +88,22 @@ development.
end
end
+ describe "Partial stubbing" do
+ it "returns nil by default" do
+ string = "my string"
+ string.stubs(:some_method)
+
+ string.some_method # => nil
+ end
+
+ it "returns a value if you tell it to" do
+ string = "my string"
+ string.stubs(:some_method) { 'some value' }
+
+ string.some_method # => "some value"
+ end
+ end
+
describe "Message expectation" do
it "is declared with expects" do
string = "my string"
@@ -156,6 +172,10 @@ development.
* is declared with fake
* is declared with double as well
* can be given stubs
+
+ Partial stubbing
+ * returns nil by default
+ * returns a value if you tell it to
Message expectation
* is declared with expects
@@ -164,7 +184,7 @@ development.
* can return a stubbed proc
* is declared with does_not_expect in case it is negative [FAILED]
- 16 examples, 4 failures, 2 pending
+ 18 examples, 4 failures, 2 pending
##Feedback
@@ -61,3 +61,41 @@ Feature: Test doubles and stubs
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"
@@ -1,3 +1,4 @@
class Object
include Stendhal::Mocks::Mockable
+ include Stendhal::Mocks::Stubbable
end
View
@@ -1,5 +1,6 @@
require 'stendhal/mocks/test_double'
require 'stendhal/mocks/mock_verifier'
+require 'stendhal/mocks/stubbable'
require 'stendhal/mocks/mockable'
module Stendhal
@@ -48,7 +48,6 @@ def times
def and_returns(retval = nil, &block)
raise "This object has no mocks." unless @__verifier
method = __verifier.last_mocked_method
- str = retval.to_s
unless respond_to?(:"__unstubbed_#{method}")
metaclass = (class << self;self;end)
metaclass.send(:alias_method, :"__unstubbed_#{method}", :"__original_#{method}")
@@ -0,0 +1,22 @@
+module Stendhal
+ module Mocks
+ module Stubbable
+
+ def stubs(method, &block)
+ unless respond_to?(:"__unstubbed_#{method}")
+ metaclass = (class << self;self;end)
+
+ if respond_to? method.to_sym
+ metaclass.send(:alias_method, :"__unstubbed_#{method}", method.to_sym)
+ metaclass.send(:undef_method, method.to_sym)
+ end
+
+ return_value = block || Proc.new { nil }
+ metaclass.send(:define_method, method.to_sym, return_value)
+ end
+ self
+ end
+
+ end
+ end
+end
View
@@ -1,3 +1,3 @@
module Stendhal
- VERSION = "0.1.6"
+ VERSION = "0.1.7"
end
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+class MyArray < Array
+ include Stendhal::Mocks::Stubbable
+end
+
+module Stendhal
+ module Mocks
+ describe "a Stubbable class" do
+
+ subject { MyArray.new([1,2,3]) }
+
+ describe "#stubs" do
+ it 'stubs an existing method and makes it return nil' do
+ subject.stubs(:length)
+ subject.length.should be_nil
+ end
+ it 'stubs an unexisting method and makes it return nil' do
+ subject.stubs(:w00t)
+ subject.w00t.should be_nil
+ end
+ it 'saves the unstubbed method' do
+ subject.stubs(:length)
+ subject.should respond_to(:__unstubbed_length)
+ subject.send(:__unstubbed_length).should == 3
+ end
+ context "with a block" do
+ it 'makes the stub return the result of the block' do
+ subject.stubs(:w00t) { 3 + 4 }
+ subject.w00t.should eq(7)
+ end
+ end
+ end
+
+ end
+ end
+end

0 comments on commit 4d43184

Please sign in to comment.