Permalink
Browse files

Adding some options on the both fake creation methods to specify init…

…ial method calls and return values for faster fake creation
  • Loading branch information...
1 parent 7a04a96 commit cde3fba77c9f24b2390187af2404abb2d09c373f @developwithpassion committed Jun 12, 2012
Showing with 22 additions and 5 deletions.
  1. +6 −4 lib/fakes.rb
  2. +16 −1 spec/specs/extensions_spec.rb
View
@@ -15,14 +15,16 @@
require 'singleton'
class Object
- def fake
- return Fakes::Fake.new
+ def fake(invocations = {})
+ item = Fakes::Fake.new
+ invocations.each{|method,return_value| item.stub(method).and_return(return_value)}
+ item
end
def arg_match
return Fakes::Matches
end
- def fake_class(klass)
- item = fake
+ def fake_class(klass,invocations = {})
+ item = fake(invocations)
Fakes::ClassSwaps.instance.add_fake_for(klass,item)
item
end
@@ -4,6 +4,11 @@
it "should be able to create a new fake" do
fake.class.should == Fakes::Fake
end
+ it "should be able to create a new fake and specify return values for methods" do
+ item = fake :hello => 'World', :age => 33
+ item.hello.should == 'World'
+ item.age.should == 33
+ end
context "when specifying a fake for a class" do
let(:swaps){fake}
@@ -21,7 +26,17 @@
it "should return the newly created fake" do
@result.should_not be_nil
end
-
+ end
+ context "when faking a class and specifying return values" do
+ before (:each) do
+ fake_class Dir,:exist? => true
+ end
+ after (:each) do
+ reset_fake_classes
+ end
+ it "should replace the class with the fake return to return the values specified" do
+ Dir.exist?('hello').should be_true
+ end
end
context "resetting fake classes" do

0 comments on commit cde3fba

Please sign in to comment.