Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5f187e
commit fee8d4d
Showing
1 changed file
with
23 additions
and
10 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 |
---|---|---|
@@ -1,31 +1,44 @@ | ||
Feature: stub_chain | ||
|
||
The stub_chain method lets you to stub a chain of methods in one statement. | ||
Method chains are considered a design smell, but it's not really the method | ||
chain that is the problem - it's the dependency chain represented by a chain | ||
of messages to different objects: | ||
|
||
foo.get_bar.get_baz | ||
|
||
This is a Law of Demeter violation if get_bar() returns an object other than | ||
foo, and get_baz() returns yet another object. | ||
|
||
Fluent interfaces look similar from a caller's perspective, but don't | ||
represent a dependency chain (the caller depends only on the object it is | ||
calling). Consider this common example from Ruby on Rails: | ||
|
||
Article.recent.by(current_user) | ||
|
||
The recent() and by() methods return the same object, so this is not | ||
a Law of Demeter violation. | ||
|
||
Scenario: stub a chain of methods | ||
Given a file named "stub_chain_spec.rb" with: | ||
""" | ||
describe "stubbing a chain of methods" do | ||
subject { Object.new } | ||
context "given symbols as methods" do | ||
context "given symbols representing methods" do | ||
it "returns the correct value" do | ||
subject.stub_chain(:one, :two, :three).and_return(:four) | ||
subject.one.two.three.should eql(:four) | ||
subject.one.two.three.should eq(:four) | ||
end | ||
end | ||
context "given a string of methods separated by dots" do | ||
it "returns the correct value" do | ||
subject.stub_chain("one.two.three").and_return(:four) | ||
subject.one.two.three.should eql(:four) | ||
subject.one.two.three.should eq(:four) | ||
end | ||
end | ||
end | ||
""" | ||
When I run "rspec stub_chain_spec.rb" | ||
Then the output should contain "2 examples, 0 failures" | ||
Then the output should contain "2 examples, 0 failures" |