-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add argument_matchers features for explicit arguments, anything, any_…
…args, and no_args
- Loading branch information
Showing
4 changed files
with
197 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
### Introduction | ||
|
||
Argument matchers can be used: | ||
|
||
* In stubs to constrain the scope of the stubbed method | ||
|
||
obj.stub(:foo).with(:bar) do |arg| | ||
#do something for :bar | ||
end | ||
obj.stub(:foo).with(:baz) do |arg| | ||
#do something for :baz | ||
end | ||
|
||
* In expectations to validate the arguments that should be received in a method call | ||
|
||
#create a double | ||
obj = double() | ||
#expect a message with given args | ||
obj.should_receive(:message).with('an argument') | ||
|
||
If more control is needed, one can use a block | ||
|
||
obj.should_receive(:message) do |arg1, arg2| | ||
# set expectations about the args in this block | ||
# and optionally 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Feature: explicit arguments | ||
|
||
Allows you to explicitly specify the argument values | ||
|
||
Scenario: explicit arguments | ||
Given a file named "stub_explicit_args_spec.rb" with: | ||
""" | ||
describe "stubbed explicit arguments" do | ||
it "works on stubs" do | ||
object = Object.new | ||
object.stub(:foo).with(:this) do |arg| | ||
"got this" | ||
end | ||
object.stub(:foo).with(:that) do |arg| | ||
"got that" | ||
end | ||
object.foo(:this).should eq("got this") | ||
object.foo(:that).should eq("got that") | ||
end | ||
it "works on doubles and expectations" do | ||
object = double('foo') | ||
object.should_receive(:bar).with(:foo) | ||
object.bar(:foo) | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_explicit_args_spec.rb` | ||
Then the output should contain "2 examples, 0 failures" | ||
|
||
Scenario: explicit arguments with multiple arities | ||
Given a file named "stub_multiple_explicit_args_spec.rb" with: | ||
""" | ||
describe "stubbed multiple explicit arguments" do | ||
it "works on stubs" do | ||
object = Object.new | ||
object.stub(:foo).with(:this) do |arg| | ||
"got this" | ||
end | ||
object.stub(:foo).with(:this, :that) do |arg1, arg2| | ||
"got this and that" | ||
end | ||
object.foo(:this).should eq("got this") | ||
object.foo(:this, :that).should eq("got this and that") | ||
end | ||
it "works on mocks" do | ||
object = double('foo') | ||
object.should_receive(:foo).with(:this, :that) | ||
object.foo(:this, :that) | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_multiple_explicit_args_spec.rb` | ||
Then the output should contain "2 examples, 0 failures" | ||
|
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
Feature: General matchers | ||
|
||
The `anything`, `any_args`, and `no_args` matchers can be used to require the method | ||
to have arguments (or not) without constraining the details of the argument, such as its | ||
type, pattern or value. The `anything` matcher only reflects a single argument, while | ||
the `any_args` matcher matches any arity. | ||
|
||
Scenario: anything argument matcher | ||
Given a file named "stub_anything_args_spec.rb" with: | ||
""" | ||
describe "stubbed anything() args spec" do | ||
it "works" do | ||
object = Object.new | ||
object.stub(:foo).with(anything) do | ||
"anything" | ||
end | ||
object.foo(1).should eq("anything") | ||
object.foo(:that).should eq("anything") | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_anything_args_spec.rb` | ||
Then the output should contain "1 example, 0 failures" | ||
|
||
Scenario: any_args argument matcher | ||
Given a file named "stub_any_args_spec.rb" with: | ||
""" | ||
describe "stubbed any_args() args spec" do | ||
it "works" do | ||
object = Object.new | ||
object.stub(:foo).with(any_args) do | ||
"anything" | ||
end | ||
object.foo(1).should eq("anything") | ||
object.foo(:that).should eq("anything") | ||
object.foo.should eq("anything") | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_any_args_spec.rb` | ||
Then the output should contain "1 example, 0 failures" | ||
|
||
Scenario: no_args argument matcher | ||
Given a file named "stub_no_args_spec.rb" with: | ||
""" | ||
describe "stubbed no_args() args spec" do | ||
it "works for no args" do | ||
object = Object.new | ||
object.stub(:foo).with(no_args) do | ||
"nothing" | ||
end | ||
object.stub(:foo).with(anything) do | ||
"something" | ||
end | ||
object.foo(:that).should eq("something") | ||
object.foo.should eq("nothing") | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_no_args_spec.rb` | ||
Then the output should contain "1 example, 0 failures" | ||
|
||
Scenario: no_args argument matcher for expectations | ||
Given a file named "stub_no_args_expectations_spec.rb" with: | ||
""" | ||
describe "stubbed no_args() args spec for expectations" do | ||
it "works for no args" do | ||
object = Object.new | ||
object.should_receive(:foo).with(no_args) | ||
object.foo | ||
end | ||
it "fails for args" do | ||
object = Object.new | ||
object.should_receive(:foo).with(no_args) | ||
object.foo(:bar) | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_no_args_expectations_spec.rb` | ||
Then the output should contain "2 examples, 1 failure" |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Feature: stub with argument constraints | ||
|
||
You can further specify the behavior by constraining the type, format and/or number of arguments with the #with() method chained off of #stub() | ||
|
||
Scenario: an_instance_of argument matcher | ||
Given a file named "stub_an_instance_of_args_spec.rb" with: | ||
""" | ||
describe "stubbed an_instance_of() args spec" do | ||
it "works" do | ||
object = Object.new | ||
object.stub(:foo).with(an_instance_of(Symbol)) do | ||
"symbol" | ||
end | ||
object.stub(:foo).with(an_instance_of(String)) do | ||
"string" | ||
end | ||
object.foo("bar").should eq("string") | ||
object.foo(:that).should eq("symbol") | ||
end | ||
end | ||
""" | ||
When I run `rspec stub_an_instance_of_args_spec.rb` | ||
Then the output should contain "1 example, 0 failures" | ||
|