-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
syntax cheat sheets for stubs and messae expectations
- Loading branch information
1 parent
3c9bf80
commit c42ea70
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
We're working on improving these docs. In the mean time, here's a cheat sheet | |||
to cover the basics. | |||
|
|||
# create a double | |||
obj = double() | |||
|
|||
# expect a message | |||
obj.should_receive(:message) | |||
|
|||
# specify a return value | |||
obj.should_receive(:message) { 'this is the value to return' } | |||
|
|||
### Argument constraints | |||
|
|||
#### Explicit arguments | |||
|
|||
obj.should_receive(:message).with('an argument') | |||
obj.should_receive(:message).with('more_than', 'one_argument') | |||
|
|||
#### Argument matchers | |||
|
|||
obj.should_receive(:message).with(anything()) | |||
obj.should_receive(:message).with(an_instance_of(Money)) | |||
obj.should_receive(:message).with(hash_including(:a => 'b')) | |||
|
|||
#### Regular expressions | |||
|
|||
obj.should_receive(:message).with(/abc/) | |||
|
|||
### Counts | |||
|
|||
obj.should_receive(:message).once | |||
obj.should_receive(:message).twice | |||
obj.should_receive(:message).exactly(3).times | |||
|
|||
obj.should_receive(:message).at_least(:once) | |||
obj.should_receive(:message).at_least(:twice) | |||
obj.should_receive(:message).at_least(n).times | |||
|
|||
obj.should_receive(:message).at_most(:once) | |||
obj.should_receive(:message).at_most(:twice) | |||
obj.should_receive(:message).at_most(n).times | |||
|
|||
### Raising/Throwing | |||
|
|||
obj.should_receive(:message) { raise "this error" } | |||
obj.should_receive(:message) { throw :this_symbol } | |||
|
|||
### Ordering | |||
|
|||
obj.should_receive(:one).ordered | |||
obj.should_receive(:two).ordered | |||
|
|||
### Arbitrary handling | |||
|
|||
obj.should_receive(:message) do |arg1, arg2| | |||
# set expectations about the args in this block | |||
# and set a return value | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,40 @@ | |||
We're working on improving these docs. In the mean time, here's a cheat sheet | |||
to cover the basics. | |||
|
|||
# create a double | |||
obj = double() | |||
|
|||
# stub a method | |||
obj.stub(:message) # returns obj | |||
|
|||
# specify a return value | |||
obj.stub(:message) { 'this is the value to return' } | |||
|
|||
### Argument constraints | |||
|
|||
#### Explicit arguments | |||
|
|||
obj.stub(:message).with('an argument') | |||
obj.stub(:message).with('more_than', 'one_argument') | |||
|
|||
#### Argument matchers | |||
|
|||
obj.stub(:message).with(anything()) | |||
obj.stub(:message).with(an_instance_of(Money)) | |||
obj.stub(:message).with(hash_including(:a => 'b')) | |||
|
|||
#### Regular expressions | |||
|
|||
obj.stub(:message).with(/abc/) | |||
|
|||
### Raising/Throwing | |||
|
|||
obj.stub(:message) { raise "this error" } | |||
obj.stub(:message) { throw :this_symbol } | |||
|
|||
### Arbitrary handling | |||
|
|||
obj.stub(:message) do |arg1, arg2| | |||
# set expectations about the args in this block | |||
# and set a return value | |||
end |