Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update docs, add spec for blocks case

  • Loading branch information...
commit 3c6fc1814b8715464a9bb91618037c76bcb5e91d 1 parent b75abd8
@siuying authored
Showing with 43 additions and 0 deletions.
  1. +16 −0 README.md
  2. +27 −0 spec/main_spec.rb
View
16 README.md
@@ -6,11 +6,27 @@ Stubbing and mocking for RubyMotion. Based on Jeremy McAnally's [stump](https://
### Stubbing right on the object
+Stubing method without parameters:
+
```ruby
# Returns 'hey!'
MyClass.stub!(:thing, :return => "hey!")
+
# Returns nil
your_object.stub!(:hello)
+
+your_object.stub!(:thing) do |arg1, arg2|
+ "Hello #{arg1},#{arg2}"
+end
+# Returns 'Hello 1,2'
+MyClass.thing("1", "2")
+
+
+your_object.stub!(:hello, :yield => ["1", "2"])
+# Yield with "1" and "2"
+your_object.hello do |a, b|
+ puts "#{a}, #{b}"
+end
```
### Pure stub objects
View
27 spec/main_spec.rb
@@ -42,6 +42,16 @@ def fetch(&block)
my_obj.stub!(:hello, return: "hi")
my_obj.hello.should.be == "hi"
end
+
+ it "should stub using block" do
+ my_obj = Object.new
+ my_obj.stub!(:hello) do |a, b|
+ a.should == "foo"
+ b.should == "bar"
+ "#{a},#{b}"
+ end
+ my_obj.hello("foo", "bar").should.be == "foo,bar"
+ end
end
describe "#stub" do
@@ -49,6 +59,15 @@ def fetch(&block)
my_stub = stub(:thing, return: "dude, a thing!")
my_stub.thing.should == "dude, a thing!"
end
+
+ it "should create a stub using block" do
+ my_stub = stub(:thing) do |a, b|
+ a.should == "a"
+ b.should == "thing!"
+ "dude, #{a} #{b}"
+ end
+ my_stub.thing("a", "thing!").should == "dude, a thing!"
+ end
end
describe "#mock!" do
@@ -59,6 +78,14 @@ def fetch(&block)
@dog.eat("broccoli").should == "Yuck!"
end
+ it "should mock using block" do
+ @dog.mock!(:bark) do |a|
+ a.should == "Meow!"
+ a
+ end
+ @dog.bark("Meow!").should == "Meow!"
+ end
+
it "should mock a class method" do
Dog.mock!(:create, return: "Dog")
Dog.create.should == "Dog"
Please sign in to comment.
Something went wrong with that request. Please try again.