Browse files

Tweak the "stub with arguments" cuke

- #80.
  • Loading branch information...
1 parent fb854a6 commit 7a2f273f8a9722b23432528df4146512f169715a @dchelimsky dchelimsky committed Oct 9, 2011
View
1 features/.nav
@@ -5,6 +5,7 @@
- simple_return_value.feature
- stub_implementation.feature
- stub_chain.feature
+ - stub_with_arguments.feature
- any_instance.feature
- as_null_object.feature
- to_ary.feature
View
2 features/method_stubs/stub_implementation.feature
@@ -1,7 +1,7 @@
Feature: stub with substitute implementation
You can stub an implementation of a method (a.k.a. fake) by passing a block
- to the stub() method.
+ to the `stub` method.
Scenario: stub implementation
Given a file named "stub_implementation_spec.rb" with:
View
69 features/method_stubs/stub_with_arguments.feature
@@ -1,70 +1,59 @@
Feature: stub with arguments
- You can set up more specific stubs by explicitly declaring the arguments the
- method stub can be invoked with.
+ Use `with` to constrain stubs to calls with specific arguments. This works
+ like a message expectation with `any_number_of_times`: it will cause a
+ failure if invoked with a different argument, but it will not cause a failure
+ if it is not invoked.
- Scenario: the stub argument is not defined
- Given a file named "stub_with_arguments_spec.rb" with:
+ Background:
+ Given a file named "account.rb" with:
"""
class Account
- def open(logger)
- logger.log :open
+ def initialize(logger)
+ @logger = logger
end
- end
-
- describe Account do
- subject { Account.new }
- it "can open an account" do
- logger = double('logger')
- logger.stub(:log)
- subject.open logger
+ def open
+ @logger.log :open
end
end
"""
- When I run `rspec stub_with_arguments_spec.rb`
- Then the examples should all pass
- Scenario: the stub argument is defined
- Given a file named "stub_with_arguments_spec.rb" with:
+ Scenario: method invoked with expected argument
+ Given a file named "example_spec.rb" with:
"""
- class Account
- def open(logger)
- logger.log :open
- end
- end
+ require "./account.rb"
describe Account do
- subject { Account.new }
-
it "can open an account" do
logger = double('logger')
+ account = Account.new(logger)
logger.stub(:log).with(:open)
- subject.open logger
+ account.open
end
end
"""
- When I run `rspec stub_with_arguments_spec.rb`
+ When I run `rspec example_spec.rb`
Then the examples should all pass
- Scenario: the stub argument is defined but it's other than the actual value
- Given a file named "stub_with_arguments_spec.rb" with:
+ Scenario: method invoked with a different argument
+ Given a file named "example_spec.rb" with:
"""
- class Account
- def open(logger)
- logger.log :open
- end
- end
+ require "./account.rb"
describe Account do
- subject { Account.new }
-
it "can open an account" do
logger = double('logger')
- logger.stub(:log).with(:something_different)
- subject.open logger
+ account = Account.new(logger)
+ logger.stub(:log).with(:open_account)
+ account.open
end
end
"""
- When I run `rspec stub_with_arguments_spec.rb`
- Then the output should contain "1 example, 1 failure"
+ When I run `rspec example_spec.rb`
+ Then the output should contain "1 failure"
+ And the output should contain:
+ """
+ expected: (:open_account)
+ got: (:open)
+ """

0 comments on commit 7a2f273

Please sign in to comment.