Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rspec/rspec-mocks
...
head fork: rspec/rspec-mocks
  • 13 commits
  • 13 files changed
  • 0 commit comments
  • 2 contributors
Commits on May 04, 2012
@dchelimsky dchelimsky rename some things in a spec d4b983c
@dchelimsky dchelimsky Fixed bug where at_least(0) failed when message was received
- Fixes #132
6b188a8
@dchelimsky dchelimsky Make sure at_least(0) passes even when never called
- Fixes #132 again
73be258
Commits on May 05, 2012
@dchelimsky dchelimsky dev: config for :focus 877dfa6
@dchelimsky dchelimsky dev: refactor a spec 2e9d990
@dchelimsky dchelimsky should_not_receive ignores and_return
- See #132
70aaf1c
@dchelimsky dchelimsky bump to 2.10.1 112b430
Commits on May 09, 2012
@dchelimsky dchelimsky Expose ArgumentListMatcher as a formal API
- supports use by 3rd party mock frameworks like Surrogate.
5d3e1dc
Commits on May 13, 2012
@dchelimsky dchelimsky spec refactoring: move shared examples to the one spec using them. eb12635
@dchelimsky dchelimsky dev: use :unless filter instead of a global exclusion filter e03d66b
@dchelimsky dchelimsky dev: rename context 671076d
@dchelimsky dchelimsky spec refactoring: rename subject with intention revealing name
- even though 'object' is no less generic than 'subject', it makes it
  more clear that we're talking about a generic object.
eebee30
Commits on May 17, 2012
@myronmarston myronmarston Fix a regression.
When doing something like:

  double.stub(:foo).and_return(a, b)
  double.should_receive(:foo).once { a }
  double.foo

...it used to work, but in 2.10 it raises a NoMethodError (#size).
132f99c
View
17 Changelog.md
@@ -1,3 +1,20 @@
+### dev
+
+Enhancements
+
+* expose ArgumentListMatcher as a formal API
+ * supports use by 3rd party mock frameworks like Surrogate
+
+### 2.10.1 / 2012-05-05
+[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.10.0...v2.10.1)
+
+Bug fixes
+
+* fix regression of edge case behavior
+ (https://github.com/rspec/rspec-mocks/issues/132)
+ * fixed failure of `object.should_receive(:message).at_least(0).times.and_return value`
+ * fixed failure of `object.should_not_receive(:message).and_return value`
+
### 2.10.0 / 2012-05-03
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.9.0...v2.10.0)
View
52 lib/rspec/mocks/argument_expectation.rb
@@ -1,52 +0,0 @@
-module RSpec
- module Mocks
- # @api private
- class ArgumentExpectation
- attr_reader :args
-
- def initialize(*args, &block)
- @args = args
- @block = args.empty? ? block : nil
- @match_any_args = false
- @matchers = nil
-
- case args.first
- when ArgumentMatchers::AnyArgsMatcher
- @match_any_args = true
- when ArgumentMatchers::NoArgsMatcher
- @matchers = []
- else
- @matchers = args.collect {|arg| matcher_for(arg)}
- end
- end
-
- def matcher_for(arg)
- return ArgumentMatchers::MatcherMatcher.new(arg) if is_matcher?(arg)
- return ArgumentMatchers::RegexpMatcher.new(arg) if arg.is_a?(Regexp)
- return ArgumentMatchers::EqualityProxy.new(arg)
- end
-
- def is_matcher?(obj)
- !obj.null_object? & obj.respond_to?(:matches?) & [:failure_message_for_should, :failure_message].any? { |m| obj.respond_to?(m) }
- end
-
- def args_match?(*args)
- match_any_args? || block_passes?(*args) || matchers_match?(*args)
- end
-
- private
-
- def block_passes?(*args)
- @block.call(*args) if @block
- end
-
- def matchers_match?(*args)
- @matchers == args
- end
-
- def match_any_args?
- @match_any_args
- end
- end
- end
-end
View
93 lib/rspec/mocks/argument_list_matcher.rb
@@ -0,0 +1,93 @@
+require 'rspec/mocks/argument_matchers'
+
+module RSpec
+ module Mocks
+ # Wrapper for matching arguments against a list of expected values. Used by
+ # the `with` method on a `MessageExpectation`:
+ #
+ # object.should_receive(:message).with(:a, 'b', 3)
+ # object.message(:a, 'b', 3)
+ #
+ # Values passed to `with` can be literal values or argument matchers that
+ # match against the real objects .e.g.
+ #
+ # object.should_receive(:message).with(hash_including(:a => 'b'))
+ #
+ # Can also be used directly to match the contents of any `Array`. This
+ # enables 3rd party mocking libs to take advantage of rspec's argument
+ # matching without using the rest of rspec-mocks.
+ #
+ # require 'rspec/mocks/argument_list_matcher'
+ # include RSpec::Mocks::ArgumentMatchers
+ #
+ # arg_list_matcher = RSpec::Mocks::ArgumentListMatcher.new(123, hash_including(:a => 'b'))
+ # arg_list_matcher.args_match?(123, :a => 'b')
+ #
+ # @see ArgumentMatchers
+ class ArgumentListMatcher
+ # @private
+ attr_reader :expected_args
+
+ # @api public
+ # @param [Array] *expected_args a list of expected literals and/or argument matchers
+ # @param [Block] block a block with arity matching the expected
+ #
+ # Initializes an `ArgumentListMatcher` with a collection of literal
+ # values and/or argument matchers, or a block that handles the evaluation
+ # for you.
+ #
+ # @see ArgumentMatchers
+ # @see #args_match?
+ def initialize(*expected_args, &block)
+ @expected_args = expected_args
+ @block = expected_args.empty? ? block : nil
+ @match_any_args = false
+ @matchers = nil
+
+ case expected_args.first
+ when ArgumentMatchers::AnyArgsMatcher
+ @match_any_args = true
+ when ArgumentMatchers::NoArgsMatcher
+ @matchers = []
+ else
+ @matchers = expected_args.collect {|arg| matcher_for(arg)}
+ end
+ end
+
+ # @api public
+ # @param [Array] *args
+ #
+ # Matches each element in the `expected_args` against the element in the same
+ # position of the arguments passed to `new`.
+ #
+ # @see #initialize
+ def args_match?(*args)
+ match_any_args? || block_passes?(*args) || matchers_match?(*args)
+ end
+
+ private
+
+ def matcher_for(arg)
+ return ArgumentMatchers::MatcherMatcher.new(arg) if is_matcher?(arg)
+ return ArgumentMatchers::RegexpMatcher.new(arg) if arg.is_a?(Regexp)
+ return ArgumentMatchers::EqualityProxy.new(arg)
+ end
+
+ def is_matcher?(obj)
+ !obj.null_object? & obj.respond_to?(:matches?) & [:failure_message_for_should, :failure_message].any? { |m| obj.respond_to?(m) }
+ end
+
+ def block_passes?(*args)
+ @block.call(*args) if @block
+ end
+
+ def matchers_match?(*args)
+ @matchers == args
+ end
+
+ def match_any_args?
+ @match_any_args
+ end
+ end
+ end
+end
View
36 lib/rspec/mocks/argument_matchers.rb
@@ -7,6 +7,8 @@ module Mocks
#
# With the exception of `any_args` and `no_args`, they all match against
# the arg in same position in the argument list.
+ #
+ # @see ArgumentListMatcher
module ArgumentMatchers
class AnyArgsMatcher
@@ -131,26 +133,26 @@ def ==(actual)
end
end
- # Passes if object receives `:message` with any args at all. This is
- # really a more explicit variation of `object.should_receive(:message)`
+ # Matches any args at all. Supports a more explicit variation of
+ # `object.should_receive(:message)`
#
# @example
#
- # object.should_receive(:message).with(any_args())
+ # object.should_receive(:message).with(any_args)
def any_args
AnyArgsMatcher.new
end
- # Passes as long as there is an argument.
+ # Matches any argument at all.
#
# @example
#
- # object.should_receive(:message).with(anything())
+ # object.should_receive(:message).with(anything)
def anything
AnyArgMatcher.new(nil)
end
- # Passes if no arguments are passed along with the message
+ # Matches no arguments.
#
# @example
#
@@ -159,7 +161,7 @@ def no_args
NoArgsMatcher.new
end
- # Passes if the argument responds to the specified messages.
+ # Matches if the actual argument responds to the specified messages.
#
# @example
#
@@ -169,7 +171,7 @@ def duck_type(*args)
DuckTypeMatcher.new(*args)
end
- # Passes if the argument is boolean.
+ # Matches a boolean value.
#
# @example
#
@@ -178,8 +180,8 @@ def boolean
BooleanMatcher.new(nil)
end
- # Passes if the argument is a hash that includes the specified key(s) or
- # key/value pairs. If the hash includes other keys, it will still pass.
+ # Matches a hash that includes the specified key(s) or key/value pairs.
+ # Ignores any additional keys.
#
# @example
#
@@ -190,8 +192,7 @@ def hash_including(*args)
HashIncludingMatcher.new(anythingize_lonely_keys(*args))
end
- # Passes if the argument is a hash that doesn't include the specified
- # key(s) or key/value
+ # Matches a hash that doesn't include the specified key(s) or key/value.
#
# @example
#
@@ -204,14 +205,21 @@ def hash_excluding(*args)
alias_method :hash_not_including, :hash_excluding
- # Passes if `arg.instance_of?(klass)`
+ # Matches if `arg.instance_of?(klass)`
+ #
+ # @example
+ #
+ # object.should_receive(:message).with(instance_of(Thing))
def instance_of(klass)
InstanceOf.new(klass)
end
alias_method :an_instance_of, :instance_of
- # Passes if `arg.kind_of?(klass)`
+ # Matches if `arg.kind_of?(klass)`
+ # @example
+ #
+ # object.should_receive(:message).with(kind_of(Thing))
def kind_of(klass)
KindOf.new(klass)
end
View
2  lib/rspec/mocks/framework.rb
@@ -9,7 +9,7 @@
require 'rspec/mocks/proxy'
require 'rspec/mocks/test_double'
require 'rspec/mocks/mock'
-require 'rspec/mocks/argument_expectation'
+require 'rspec/mocks/argument_list_matcher'
require 'rspec/mocks/message_expectation'
require 'rspec/mocks/order_group'
require 'rspec/mocks/errors'
View
34 lib/rspec/mocks/message_expectation.rb
@@ -4,7 +4,7 @@ module Mocks
class MessageExpectation
# @private
attr_reader :message
- attr_writer :expected_received_count, :expected_from, :argument_expectation, :implementation
+ attr_writer :expected_received_count, :expected_from, :argument_list_matcher, :implementation
protected :expected_received_count=, :expected_from=, :implementation=
attr_accessor :error_generator
protected :error_generator, :error_generator=
@@ -17,7 +17,7 @@ def initialize(error_generator, expectation_ordering, expected_from, message, ex
@message = message
@actual_received_count = 0
@expected_received_count = expected_received_count
- @argument_expectation = ArgumentExpectation.new(ArgumentMatchers::AnyArgsMatcher.new)
+ @argument_list_matcher = ArgumentListMatcher.new(ArgumentMatchers::AnyArgsMatcher.new)
@consecutive = false
@exception_to_raise = nil
@args_to_throw = []
@@ -34,20 +34,28 @@ def initialize(error_generator, expectation_ordering, expected_from, message, ex
def build_child(expected_from, expected_received_count, opts={}, &implementation)
child = clone
child.expected_from = expected_from
- child.implementation = implementation if implementation
+
+ if implementation
+ child.implementation = implementation
+
+ # A new block implementation is given, so the old multi-return
+ # implementation needs to be disabled
+ child.instance_variable_set(:@consecutive, false)
+ end
+
child.expected_received_count = expected_received_count
child.clear_actual_received_count!
new_gen = error_generator.clone
new_gen.opts = opts
child.error_generator = new_gen
child.clone_args_to_yield(*@args_to_yield)
- child.argument_expectation = ArgumentExpectation.new(ArgumentMatchers::AnyArgsMatcher.new)
+ child.argument_list_matcher = ArgumentListMatcher.new(ArgumentMatchers::AnyArgsMatcher.new)
child
end
# @private
def expected_args
- @argument_expectation.args
+ @argument_list_matcher.expected_args
end
# @overload and_return(value)
@@ -87,7 +95,7 @@ def expected_args
# counter.stub(:count) { 1 }
# counter.count # => 1
def and_return(*values, &implementation)
- @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args?
+ @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args? || (@expected_received_count == 0 and @at_least)
@consecutive = true if values.size > 1
@implementation = implementation || build_implementation(values)
end
@@ -148,12 +156,12 @@ def and_yield(*args, &block)
# @private
def matches?(message, *args)
- @message == message && @argument_expectation.args_match?(*args)
+ @message == message && @argument_list_matcher.args_match?(*args)
end
# @private
def invoke(*args, &block)
- if @expected_received_count == 0 || ((@exactly || @at_most) && (@actual_received_count == @expected_received_count))
+ if (@expected_received_count == 0 && !@at_least) || ((@exactly || @at_most) && (@actual_received_count == @expected_received_count))
@actual_received_count += 1
@failed_fast = true
@error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, *args)
@@ -199,7 +207,7 @@ def called_max_times?
# @private
def matches_name_but_not_args(message, *args)
- @message == message and not @argument_expectation.args_match?(*args)
+ @message == message and not @argument_list_matcher.args_match?(*args)
end
# @private
@@ -248,7 +256,7 @@ def advise(*args)
# @private
def generate_error
if similar_messages.empty?
- @error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, *@argument_expectation.args)
+ @error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, *expected_args)
else
@error_generator.raise_similar_message_args_error(self, *@similar_messages)
end
@@ -284,7 +292,7 @@ def raise_out_of_order_error
# # => passes
def with(*args, &block)
@implementation = block if block_given? unless args.empty?
- @argument_expectation = ArgumentExpectation.new(*args, &block)
+ @argument_list_matcher = ArgumentListMatcher.new(*args, &block)
self
end
@@ -467,6 +475,10 @@ def initialize(error_generator, expectation_ordering, expected_from, message, &i
super(error_generator, expectation_ordering, expected_from, message, 0, {}, &implementation)
end
+ def and_return(*)
+ # no-op
+ end
+
# @private
def negative_expectation_for?(message)
return @message == message
View
2  lib/rspec/mocks/version.rb
@@ -1,7 +1,7 @@
module RSpec
module Mocks
module Version
- STRING = '2.10.0'
+ STRING = '2.10.1'
end
end
end
View
14 spec/rspec/mocks/argument_expectation_spec.rb
@@ -2,32 +2,32 @@
module RSpec
module Mocks
- describe ArgumentExpectation do
+ describe ArgumentListMatcher do
it "considers an object that responds to #matches? and #failure_message_for_should to be a matcher" do
- argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
+ argument_expectation = RSpec::Mocks::ArgumentListMatcher.new
obj = double("matcher")
obj.stub(:respond_to?).with(:matches?).and_return(true)
obj.stub(:respond_to?).with(:failure_message_for_should).and_return(true)
- argument_expecatation.is_matcher?(obj).should be_true
+ argument_expectation.send(:is_matcher?, obj).should be_true
end
it "considers an object that responds to #matches? and #failure_message to be a matcher for backward compatibility" do
- argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
+ argument_expectation = RSpec::Mocks::ArgumentListMatcher.new
obj = double("matcher")
obj.stub(:respond_to?).with(:matches?).and_return(true)
obj.stub(:respond_to?).with(:failure_message_for_should).and_return(false)
obj.stub(:respond_to?).with(:failure_message).and_return(true)
- argument_expecatation.is_matcher?(obj).should be_true
+ argument_expectation.send(:is_matcher?, obj).should be_true
end
it "does NOT consider an object that only responds to #matches? to be a matcher" do
- argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
+ argument_expectation = RSpec::Mocks::ArgumentListMatcher.new
obj = double("matcher")
obj.stub(:respond_to?).with(:matches?).and_return(true)
obj.stub(:respond_to?).with(:failure_message_for_should).and_return(false)
obj.stub(:respond_to?).with(:failure_message).and_return(false)
- argument_expecatation.is_matcher?(obj).should be_false
+ argument_expectation.send(:is_matcher?, obj).should be_false
end
end
end
View
125 spec/rspec/mocks/at_least_spec.rb
@@ -3,100 +3,125 @@
module RSpec
module Mocks
describe "at_least" do
- before(:each) do
- @mock = RSpec::Mocks::Mock.new("test mock")
- end
+ before(:each) { @double = double }
it "fails if method is never called" do
- @mock.should_receive(:random_call).at_least(4).times
- lambda do
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(4).times
+ lambda do
+ @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "fails when called less than n times" do
- @mock.should_receive(:random_call).at_least(4).times
- @mock.random_call
- @mock.random_call
- @mock.random_call
+ @double.should_receive(:do_something).at_least(4).times
+ @double.do_something
+ @double.do_something
+ @double.do_something
lambda do
- @mock.rspec_verify
+ @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "fails when at least once method is never called" do
- @mock.should_receive(:random_call).at_least(:once)
+ @double.should_receive(:do_something).at_least(:once)
lambda do
- @mock.rspec_verify
+ @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "fails when at least twice method is called once" do
- @mock.should_receive(:random_call).at_least(:twice)
- @mock.random_call
+ @double.should_receive(:do_something).at_least(:twice)
+ @double.do_something
lambda do
- @mock.rspec_verify
+ @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "fails when at least twice method is never called" do
- @mock.should_receive(:random_call).at_least(:twice)
+ @double.should_receive(:do_something).at_least(:twice)
lambda do
- @mock.rspec_verify
+ @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "passes when at least n times method is called exactly n times" do
- @mock.should_receive(:random_call).at_least(4).times
- @mock.random_call
- @mock.random_call
- @mock.random_call
- @mock.random_call
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(4).times
+ @double.do_something
+ @double.do_something
+ @double.do_something
+ @double.do_something
+ @double.rspec_verify
end
it "passes when at least n times method is called n plus 1 times" do
- @mock.should_receive(:random_call).at_least(4).times
- @mock.random_call
- @mock.random_call
- @mock.random_call
- @mock.random_call
- @mock.random_call
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(4).times
+ @double.do_something
+ @double.do_something
+ @double.do_something
+ @double.do_something
+ @double.do_something
+ @double.rspec_verify
end
it "passes when at least once method is called once" do
- @mock.should_receive(:random_call).at_least(:once)
- @mock.random_call
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(:once)
+ @double.do_something
+ @double.rspec_verify
end
it "passes when at least once method is called twice" do
- @mock.should_receive(:random_call).at_least(:once)
- @mock.random_call
- @mock.random_call
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(:once)
+ @double.do_something
+ @double.do_something
+ @double.rspec_verify
end
it "passes when at least twice method is called three times" do
- @mock.should_receive(:random_call).at_least(:twice)
- @mock.random_call
- @mock.random_call
- @mock.random_call
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(:twice)
+ @double.do_something
+ @double.do_something
+ @double.do_something
+ @double.rspec_verify
end
it "passes when at least twice method is called twice" do
- @mock.should_receive(:random_call).at_least(:twice)
- @mock.random_call
- @mock.random_call
- @mock.rspec_verify
+ @double.should_receive(:do_something).at_least(:twice)
+ @double.do_something
+ @double.do_something
+ @double.rspec_verify
end
it "returns the value given by a block when the at least once method is called" do
- @mock.should_receive(:to_s).at_least(:once) { "testing" }
- @mock.to_s.should eq "testing"
- @mock.rspec_verify
+ @double.should_receive(:to_s).at_least(:once) { "testing" }
+ @double.to_s.should eq "testing"
+ @double.rspec_verify
+ end
+
+ it "passes with at_least(0) with no return if called once" do
+ @double.should_receive(:do_something).at_least(0).times
+ @double.do_something
+ end
+
+ it "passes with at_least(0) with return block if called once" do
+ @double.should_receive(:do_something).at_least(0).times { true }
+ @double.do_something
+ end
+
+ it "passes with at_least(0) with and_return if called once" do
+ @double.should_receive(:do_something).at_least(0).times.and_return true
+ @double.do_something
+ end
+
+ it "passes with at_least(0) with no return if never called" do
+ @double.should_receive(:do_something).at_least(0).times
+ end
+
+ it "passes with at_least(0) with return block if never called" do
+ @double.should_receive(:do_something).at_least(0).times { true }
+ end
+
+ it "passes with at_least(0) with and_return if never called" do
+ @double.should_receive(:do_something).at_least(0).times.and_return true
end
end
end
View
8 spec/rspec/mocks/block_return_value_spec.rb
@@ -7,6 +7,14 @@
obj.should_receive(:foo) { 'bar' }
obj.foo.should eq('bar')
end
+
+ it "works when a multi-return stub has already been set" do
+ obj = Object.new
+ return_value = Object.new
+ obj.stub(:foo).and_return(return_value, nil)
+ obj.should_receive(:foo) { return_value }
+ obj.foo.should be(return_value)
+ end
end
describe "stub" do
View
502 spec/rspec/mocks/mock_spec.rb
@@ -3,20 +3,21 @@
module RSpec
module Mocks
describe Mock do
- before(:each) do
- @mock = double("test double")
- end
+ before(:each) { @double = double("test double") }
+ after(:each) { @double.rspec_reset }
- treats_method_missing_as_private :subject => RSpec::Mocks::Mock.new, :noop => false
+ it "has method_missing as private" do
+ RSpec::Mocks::Mock.private_instance_methods.should include_method(:method_missing)
+ end
- after(:each) do
- @mock.rspec_reset
+ it "does not respond_to? method_missing (because it's private)" do
+ RSpec::Mocks::Mock.new.should_not respond_to(:method_missing)
end
it "reports line number of expectation of unreceived message" do
- expected_error_line = __LINE__; @mock.should_receive(:wont_happen).with("x", 3)
+ expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
begin
- @mock.rspec_verify
+ @double.rspec_verify
violated
rescue RSpec::Mocks::MockExpectationError => e
# NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
@@ -25,10 +26,10 @@ module Mocks
end
it "reports line number of expectation of unreceived message after #should_receive after similar stub" do
- @mock.stub(:wont_happen)
- expected_error_line = __LINE__; @mock.should_receive(:wont_happen).with("x", 3)
+ @double.stub(:wont_happen)
+ expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
begin
- @mock.rspec_verify
+ @double.rspec_verify
violated
rescue RSpec::Mocks::MockExpectationError => e
# NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
@@ -37,21 +38,28 @@ module Mocks
end
it "passes when not receiving message specified as not to be received" do
- @mock.should_not_receive(:not_expected)
- @mock.rspec_verify
+ @double.should_not_receive(:not_expected)
+ @double.rspec_verify
+ end
+
+ it "passes when not receiving message specified as not to be received with and_return" do
+ # NOTE (DC 2012-05-05) calling `and_return` after `should_not_receive` makes no sense
+ # and should probably be disallowed.
+ @double.should_not_receive(:not_expected).and_return nil
+ @double.rspec_verify
end
it "passes when receiving message specified as not to be received with different args" do
- @mock.should_not_receive(:message).with("unwanted text")
- @mock.should_receive(:message).with("other text")
- @mock.message "other text"
- @mock.rspec_verify
+ @double.should_not_receive(:message).with("unwanted text")
+ @double.should_receive(:message).with("other text")
+ @double.message "other text"
+ @double.rspec_verify
end
it "fails when receiving message specified as not to be received" do
- @mock.should_not_receive(:not_expected)
+ @double.should_not_receive(:not_expected)
expect {
- @mock.not_expected
+ @double.not_expected
violated
}.to raise_error(
RSpec::Mocks::MockExpectationError,
@@ -60,9 +68,9 @@ module Mocks
end
it "fails when receiving message specified as not to be received with args" do
- @mock.should_not_receive(:not_expected).with("unexpected text")
+ @double.should_not_receive(:not_expected).with("unexpected text")
expect {
- @mock.not_expected("unexpected text")
+ @double.not_expected("unexpected text")
violated
}.to raise_error(
RSpec::Mocks::MockExpectationError,
@@ -71,157 +79,159 @@ module Mocks
end
it "passes when receiving message specified as not to be received with wrong args" do
- @mock.should_not_receive(:not_expected).with("unexpected text")
- @mock.not_expected "really unexpected text"
- @mock.rspec_verify
+ @double.should_not_receive(:not_expected).with("unexpected text")
+ @double.not_expected "really unexpected text"
+ @double.rspec_verify
end
it "allows block to calculate return values" do
- @mock.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
- @mock.something("a","b","c").should eq "cba"
- @mock.rspec_verify
+ @double.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
+ @double.something("a","b","c").should eq "cba"
+ @double.rspec_verify
end
it "allows parameter as return value" do
- @mock.should_receive(:something).with("a","b","c").and_return("booh")
- @mock.something("a","b","c").should eq "booh"
- @mock.rspec_verify
+ @double.should_receive(:something).with("a","b","c").and_return("booh")
+ @double.something("a","b","c").should eq "booh"
+ @double.rspec_verify
end
it "returns the previously stubbed value if no return value is set" do
- @mock.stub(:something).with("a","b","c").and_return(:stubbed_value)
- @mock.should_receive(:something).with("a","b","c")
- @mock.something("a","b","c").should eq :stubbed_value
- @mock.rspec_verify
+ @double.stub(:something).with("a","b","c").and_return(:stubbed_value)
+ @double.should_receive(:something).with("a","b","c")
+ @double.something("a","b","c").should eq :stubbed_value
+ @double.rspec_verify
end
it "returns nil if no return value is set and there is no previously stubbed value" do
- @mock.should_receive(:something).with("a","b","c")
- @mock.something("a","b","c").should be_nil
- @mock.rspec_verify
+ @double.should_receive(:something).with("a","b","c")
+ @double.something("a","b","c").should be_nil
+ @double.rspec_verify
end
it "raises exception if args don't match when method called" do
- @mock.should_receive(:something).with("a","b","c").and_return("booh")
+ @double.should_receive(:something).with("a","b","c").and_return("booh")
lambda {
- @mock.something("a","d","c")
+ @double.something("a","d","c")
violated
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
end
describe "even when a similar expectation with different arguments exist" do
it "raises exception if args don't match when method called, correctly reporting the offending arguments" do
- @mock.should_receive(:something).with("a","b","c").once
- @mock.should_receive(:something).with("z","x","c").once
+ @double.should_receive(:something).with("a","b","c").once
+ @double.should_receive(:something).with("z","x","c").once
lambda {
- @mock.something("a","b","c")
- @mock.something("z","x","g")
+ @double.something("a","b","c")
+ @double.something("z","x","g")
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"z\", \"x\", \"c\")\n got: (\"z\", \"x\", \"g\")")
end
end
it "raises exception if args don't match when method called even when the method is stubbed" do
- @mock.stub(:something)
- @mock.should_receive(:something).with("a","b","c")
+ @double.stub(:something)
+ @double.should_receive(:something).with("a","b","c")
lambda {
- @mock.something("a","d","c")
- @mock.rspec_verify
+ @double.something("a","d","c")
+ @double.rspec_verify
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
end
it "raises exception if args don't match when method called even when using null_object" do
- @mock = double("test double").as_null_object
- @mock.should_receive(:something).with("a","b","c")
+ @double = double("test double").as_null_object
+ @double.should_receive(:something).with("a","b","c")
lambda {
- @mock.something("a","d","c")
- @mock.rspec_verify
+ @double.something("a","d","c")
+ @double.rspec_verify
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
end
describe 'with a method that has a default argument' do
it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
- def @mock.method_with_default_argument(arg={}); end
- @mock.should_receive(:method_with_default_argument).with({})
+ def @double.method_with_default_argument(arg={}); end
+ @double.should_receive(:method_with_default_argument).with({})
expect {
- @mock.method_with_default_argument(nil)
- @mock.rspec_verify
+ @double.method_with_default_argument(nil)
+ @double.rspec_verify
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n expected: ({})\n got: (nil)")
end
end
it "fails if unexpected method called" do
lambda {
- @mock.something("a","b","c")
+ @double.something("a","b","c")
violated
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received unexpected message :something with (\"a\", \"b\", \"c\")")
end
it "uses block for expectation if provided" do
- @mock.should_receive(:something) do | a, b |
+ @double.should_receive(:something) do | a, b |
a.should eq "a"
b.should eq "b"
"booh"
end
- @mock.something("a", "b").should eq "booh"
- @mock.rspec_verify
+ @double.something("a", "b").should eq "booh"
+ @double.rspec_verify
end
it "fails if expectation block fails" do
- @mock.should_receive(:something) {| bool | bool.should be_true}
+ @double.should_receive(:something) {| bool | bool.should be_true}
expect {
- @mock.something false
+ @double.something false
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
- it "passes proc to expectation block without an argument", :ruby => '> 1.8.6' do
- # We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
- # and prevents the entire spec suite from running.
- eval("@mock.should_receive(:foo) {|&block| block.call.should eq(:bar)}")
- @mock.foo { :bar }
- end
+ context "with Ruby > 1.8.6", :unless => RUBY_VERSION.to_s == '1.8.6' do
+ it "passes proc to expectation block without an argument" do
+ # We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
+ # and prevents the entire spec suite from running.
+ eval("@double.should_receive(:foo) {|&block| block.call.should eq(:bar)}")
+ @double.foo { :bar }
+ end
- it "passes proc to expectation block with an argument", :ruby => '> 1.8.6' do
- eval("@mock.should_receive(:foo) {|arg, &block| block.call.should eq(:bar)}")
- @mock.foo(:arg) { :bar }
- end
+ it "passes proc to expectation block with an argument" do
+ eval("@double.should_receive(:foo) {|arg, &block| block.call.should eq(:bar)}")
+ @double.foo(:arg) { :bar }
+ end
- it "passes proc to stub block without an argurment", :ruby => '>1.8.6' do
- eval("@mock.stub(:foo) {|&block| block.call.should eq(:bar)}")
- @mock.foo { :bar }
- end
+ it "passes proc to stub block without an argurment" do
+ eval("@double.stub(:foo) {|&block| block.call.should eq(:bar)}")
+ @double.foo { :bar }
+ end
- it "passes proc to stub block with an argument", :ruby => '> 1.8.6' do
- eval("@mock.stub(:foo) {|arg, &block| block.call.should eq(:bar)}")
- @mock.foo(:arg) { :bar }
+ it "passes proc to stub block with an argument" do
+ eval("@double.stub(:foo) {|arg, &block| block.call.should eq(:bar)}")
+ @double.foo(:arg) { :bar }
+ end
end
it "fails right away when method defined as never is received" do
- @mock.should_receive(:not_expected).never
- expect { @mock.not_expected }.
+ @double.should_receive(:not_expected).never
+ expect { @double.not_expected }.
to raise_error(RSpec::Mocks::MockExpectationError,
%Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
)
end
it "raises when told to" do
- @mock.should_receive(:something).and_raise(RuntimeError)
- expect { @mock.something }.to raise_error(RuntimeError)
+ @double.should_receive(:something).and_raise(RuntimeError)
+ expect { @double.something }.to raise_error(RuntimeError)
end
it "raises instance of submitted Exception" do
error = RuntimeError.new("error message")
- @mock.should_receive(:something).and_raise(error)
+ @double.should_receive(:something).and_raise(error)
lambda {
- @mock.something
+ @double.something
}.should raise_error(RuntimeError, "error message")
end
it "raises instance of submitted ArgumentError" do
error = ArgumentError.new("error message")
- @mock.should_receive(:something).and_raise(error)
+ @double.should_receive(:something).and_raise(error)
lambda {
- @mock.something
+ @double.something
}.should raise_error(ArgumentError, "error message")
end
@@ -231,208 +241,208 @@ def initialize(i_take_an_argument)
end
end
- @mock.stub(:something).and_raise(ErrorWithNonZeroArgConstructor)
+ @double.stub(:something).and_raise(ErrorWithNonZeroArgConstructor)
lambda {
- @mock.something
+ @double.something
}.should raise_error(ArgumentError, /^'and_raise' can only accept an Exception class if an instance/)
end
it "raises RuntimeError with submitted message" do
- @mock.should_receive(:something).and_raise("error message")
+ @double.should_receive(:something).and_raise("error message")
lambda {
- @mock.something
+ @double.something
}.should raise_error(RuntimeError, "error message")
end
it "does not raise when told to if args dont match" do
- @mock.should_receive(:something).with(2).and_raise(RuntimeError)
+ @double.should_receive(:something).with(2).and_raise(RuntimeError)
lambda {
- @mock.something 1
+ @double.something 1
}.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "throws when told to" do
- @mock.should_receive(:something).and_throw(:blech)
+ @double.should_receive(:something).and_throw(:blech)
lambda {
- @mock.something
+ @double.something
}.should throw_symbol(:blech)
end
it "ignores args on any args" do
- @mock.should_receive(:something).at_least(:once).with(any_args)
- @mock.something
- @mock.something 1
- @mock.something "a", 2
- @mock.something [], {}, "joe", 7
- @mock.rspec_verify
+ @double.should_receive(:something).at_least(:once).with(any_args)
+ @double.something
+ @double.something 1
+ @double.something "a", 2
+ @double.something [], {}, "joe", 7
+ @double.rspec_verify
end
it "fails on no args if any args received" do
- @mock.should_receive(:something).with(no_args())
+ @double.should_receive(:something).with(no_args())
lambda {
- @mock.something 1
+ @double.something 1
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (no args)\n got: (1)")
end
it "fails when args are expected but none are received" do
- @mock.should_receive(:something).with(1)
+ @double.should_receive(:something).with(1)
lambda {
- @mock.something
+ @double.something
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (1)\n got: (no args)")
end
it "returns value from block by default" do
- @mock.stub(:method_that_yields).and_yield
- @mock.method_that_yields { :returned_obj }.should eq :returned_obj
- @mock.rspec_verify
+ @double.stub(:method_that_yields).and_yield
+ @double.method_that_yields { :returned_obj }.should eq :returned_obj
+ @double.rspec_verify
end
it "yields 0 args to blocks that take a variable number of arguments" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield
a = nil
- @mock.yield_back {|*x| a = x}
+ @double.yield_back {|*x| a = x}
a.should eq []
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields 0 args multiple times to blocks that take a variable number of arguments" do
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield.
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield.
and_yield
b = []
- @mock.yield_back {|*a| b << a}
+ @double.yield_back {|*a| b << a}
b.should eq [ [], [] ]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields one arg to blocks that take a variable number of arguments" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
a = nil
- @mock.yield_back {|*x| a = x}
+ @double.yield_back {|*x| a = x}
a.should eq [99]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields one arg 3 times consecutively to blocks that take a variable number of arguments" do
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
and_yield(43).
and_yield("something fruity")
b = []
- @mock.yield_back {|*a| b << a}
+ @double.yield_back {|*a| b << a}
b.should eq [[99], [43], ["something fruity"]]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields many args to blocks that take a variable number of arguments" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
a = nil
- @mock.yield_back {|*x| a = x}
+ @double.yield_back {|*x| a = x}
a.should eq [99, 27, "go"]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields many args 3 times consecutively to blocks that take a variable number of arguments" do
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
and_yield("wait", :amber).
and_yield("stop", 12, :red)
b = []
- @mock.yield_back {|*a| b << a}
+ @double.yield_back {|*a| b << a}
b.should eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields single value" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
a = nil
- @mock.yield_back {|x| a = x}
+ @double.yield_back {|x| a = x}
a.should eq 99
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields single value 3 times consecutively" do
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
and_yield(43).
and_yield("something fruity")
b = []
- @mock.yield_back {|a| b << a}
+ @double.yield_back {|a| b << a}
b.should eq [99, 43, "something fruity"]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields two values" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
a, b = nil
- @mock.yield_back {|x,y| a=x; b=y}
+ @double.yield_back {|x,y| a=x; b=y}
a.should eq 'wha'
b.should eq 'zup'
- @mock.rspec_verify
+ @double.rspec_verify
end
it "yields two values 3 times consecutively" do
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
and_yield('not', 'down').
and_yield(14, 65)
c = []
- @mock.yield_back {|a,b| c << [a, b]}
+ @double.yield_back {|a,b| c << [a, b]}
c.should eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
- @mock.rspec_verify
+ @double.rspec_verify
end
it "fails when calling yielding method with wrong arity" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
lambda {
- @mock.yield_back {|a|}
+ @double.yield_back {|a|}
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"wha\", \"zup\"| to block with arity of 1")
end
it "fails when calling yielding method consecutively with wrong arity" do
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
and_yield('down').
and_yield(14, 65)
lambda {
c = []
- @mock.yield_back {|a,b| c << [a, b]}
+ @double.yield_back {|a,b| c << [a, b]}
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
end
it "fails when calling yielding method without block" do
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
lambda {
- @mock.yield_back
+ @double.yield_back
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" asked to yield |[\"wha\", \"zup\"]| but no block was passed")
end
- it "is able to mock send" do
- @mock.should_receive(:send).with(any_args)
- @mock.send 'hi'
- @mock.rspec_verify
+ it "is able to double send" do
+ @double.should_receive(:send).with(any_args)
+ @double.send 'hi'
+ @double.rspec_verify
end
- it "is able to raise from method calling yielding mock" do
- @mock.should_receive(:yield_me).and_yield 44
+ it "is able to raise from method calling yielding double" do
+ @double.should_receive(:yield_me).and_yield 44
lambda {
- @mock.yield_me do |x|
+ @double.yield_me do |x|
raise "Bang"
end
}.should raise_error(StandardError, "Bang")
- @mock.rspec_verify
+ @double.rspec_verify
end
it "clears expectations after verify" do
- @mock.should_receive(:foobar)
- @mock.foobar
- @mock.rspec_verify
+ @double.should_receive(:foobar)
+ @double.foobar
+ @double.rspec_verify
lambda {
- @mock.foobar
+ @double.foobar
}.should raise_error(RSpec::Mocks::MockExpectationError, %q|Double "test double" received unexpected message :foobar with (no args)|)
end
it "restores objects to their original state on rspec_reset" do
- mock = double("this is a mock")
- mock.should_receive(:blah)
- mock.rspec_reset
- mock.rspec_verify #should throw if reset didn't work
+ double = double("this is a double")
+ double.should_receive(:blah)
+ double.rspec_reset
+ double.rspec_verify #should throw if reset didn't work
end
it "works even after method_missing starts raising NameErrors instead of NoMethodErrors" do
@@ -456,68 +466,68 @@ def initialize(i_take_an_argument)
a_method_that_doesnt_exist rescue
- @mock.should_receive(:foobar)
- @mock.foobar
- @mock.rspec_verify
+ @double.should_receive(:foobar)
+ @double.foobar
+ @double.rspec_verify
- lambda { @mock.foobar }.should_not raise_error(NameError)
- lambda { @mock.foobar }.should raise_error(RSpec::Mocks::MockExpectationError)
+ lambda { @double.foobar }.should_not raise_error(NameError)
+ lambda { @double.foobar }.should raise_error(RSpec::Mocks::MockExpectationError)
end
- it "temporarily replaces a method stub on a mock" do
- @mock.stub(:msg).and_return(:stub_value)
- @mock.should_receive(:msg).with(:arg).and_return(:mock_value)
- @mock.msg(:arg).should equal(:mock_value)
- @mock.msg.should equal(:stub_value)
- @mock.msg.should equal(:stub_value)
- @mock.rspec_verify
+ it "temporarily replaces a method stub on a double" do
+ @double.stub(:msg).and_return(:stub_value)
+ @double.should_receive(:msg).with(:arg).and_return(:double_value)
+ @double.msg(:arg).should equal(:double_value)
+ @double.msg.should equal(:stub_value)
+ @double.msg.should equal(:stub_value)
+ @double.rspec_verify
end
it "does not require a different signature to replace a method stub" do
- @mock.stub(:msg).and_return(:stub_value)
- @mock.should_receive(:msg).and_return(:mock_value)
- @mock.msg(:arg).should equal(:mock_value)
- @mock.msg.should equal(:stub_value)
- @mock.msg.should equal(:stub_value)
- @mock.rspec_verify
+ @double.stub(:msg).and_return(:stub_value)
+ @double.should_receive(:msg).and_return(:double_value)
+ @double.msg(:arg).should equal(:double_value)
+ @double.msg.should equal(:stub_value)
+ @double.msg.should equal(:stub_value)
+ @double.rspec_verify
end
it "raises an error when a previously stubbed method has a negative expectation" do
- @mock.stub(:msg).and_return(:stub_value)
- @mock.should_not_receive(:msg)
- expect { @mock.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
+ @double.stub(:msg).and_return(:stub_value)
+ @double.should_not_receive(:msg)
+ expect { @double.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
end
- it "temporarily replaces a method stub on a non-mock" do
- non_mock = Object.new
- non_mock.stub(:msg).and_return(:stub_value)
- non_mock.should_receive(:msg).with(:arg).and_return(:mock_value)
- non_mock.msg(:arg).should equal(:mock_value)
- non_mock.msg.should equal(:stub_value)
- non_mock.msg.should equal(:stub_value)
- non_mock.rspec_verify
+ it "temporarily replaces a method stub on a non-double" do
+ non_double = Object.new
+ non_double.stub(:msg).and_return(:stub_value)
+ non_double.should_receive(:msg).with(:arg).and_return(:double_value)
+ non_double.msg(:arg).should equal(:double_value)
+ non_double.msg.should equal(:stub_value)
+ non_double.msg.should equal(:stub_value)
+ non_double.rspec_verify
end
it "returns the stubbed value when no new value specified" do
- @mock.stub(:msg).and_return(:stub_value)
- @mock.should_receive(:msg)
- @mock.msg.should equal(:stub_value)
- @mock.rspec_verify
+ @double.stub(:msg).and_return(:stub_value)
+ @double.should_receive(:msg)
+ @double.msg.should equal(:stub_value)
+ @double.rspec_verify
end
it "returns the stubbed value when stubbed with args and no new value specified" do
- @mock.stub(:msg).with(:arg).and_return(:stub_value)
- @mock.should_receive(:msg).with(:arg)
- @mock.msg(:arg).should equal(:stub_value)
- @mock.rspec_verify
+ @double.stub(:msg).with(:arg).and_return(:stub_value)
+ @double.should_receive(:msg).with(:arg)
+ @double.msg(:arg).should equal(:stub_value)
+ @double.rspec_verify
end
- it "does not mess with the stub's yielded values when also mocked" do
- @mock.stub(:yield_back).and_yield(:stub_value)
- @mock.should_receive(:yield_back).and_yield(:mock_value)
- @mock.yield_back{|v| v.should eq :mock_value }
- @mock.yield_back{|v| v.should eq :stub_value }
- @mock.rspec_verify
+ it "does not mess with the stub's yielded values when also doubleed" do
+ @double.stub(:yield_back).and_yield(:stub_value)
+ @double.should_receive(:yield_back).and_yield(:double_value)
+ @double.yield_back{|v| v.should eq :double_value }
+ @double.yield_back{|v| v.should eq :stub_value }
+ @double.rspec_verify
end
it "yields multiple values after a similar stub" do
@@ -531,15 +541,15 @@ def initialize(i_take_an_argument)
end
it "assigns stub return values" do
- mock = RSpec::Mocks::Mock.new('name', :message => :response)
- mock.message.should eq :response
+ double = RSpec::Mocks::Mock.new('name', :message => :response)
+ double.message.should eq :response
end
end
- describe "a mock message receiving a block" do
+ describe "a double message receiving a block" do
before(:each) do
- @mock = double("mock")
+ @double = double("double")
@calls = 0
end
@@ -548,61 +558,61 @@ def add_call
end
it "calls the block after #should_receive" do
- @mock.should_receive(:foo) { add_call }
+ @double.should_receive(:foo) { add_call }
- @mock.foo
+ @double.foo
@calls.should eq 1
end
it "calls the block after #should_receive after a similar stub" do
- @mock.stub(:foo).and_return(:bar)
- @mock.should_receive(:foo) { add_call }
+ @double.stub(:foo).and_return(:bar)
+ @double.should_receive(:foo) { add_call }
- @mock.foo
+ @double.foo
@calls.should eq 1
end
it "calls the block after #once" do
- @mock.should_receive(:foo).once { add_call }
+ @double.should_receive(:foo).once { add_call }
- @mock.foo
+ @double.foo
@calls.should eq 1
end
it "calls the block after #twice" do
- @mock.should_receive(:foo).twice { add_call }
+ @double.should_receive(:foo).twice { add_call }
- @mock.foo
- @mock.foo
+ @double.foo
+ @double.foo
@calls.should eq 2
end
it "calls the block after #times" do
- @mock.should_receive(:foo).exactly(10).times { add_call }
+ @double.should_receive(:foo).exactly(10).times { add_call }
- (1..10).each { @mock.foo }
+ (1..10).each { @double.foo }
@calls.should eq 10
end
it "calls the block after #any_number_of_times" do
- @mock.should_receive(:foo).any_number_of_times { add_call }
+ @double.should_receive(:foo).any_number_of_times { add_call }
- (1..7).each { @mock.foo }
+ (1..7).each { @double.foo }
@calls.should eq 7
end
it "calls the block after #ordered" do
- @mock.should_receive(:foo).ordered { add_call }
- @mock.should_receive(:bar).ordered { add_call }
+ @double.should_receive(:foo).ordered { add_call }
+ @double.should_receive(:bar).ordered { add_call }
- @mock.foo
- @mock.bar
+ @double.foo
+ @double.bar
@calls.should eq 2
end
@@ -610,8 +620,8 @@ def add_call
describe 'string representation generated by #to_s' do
it 'does not contain < because that might lead to invalid HTML in some situations' do
- mock = double("Dog")
- valid_html_str = "#{mock}"
+ double = double("Dog")
+ valid_html_str = "#{double}"
valid_html_str.should_not include('<')
end
end
@@ -623,10 +633,10 @@ def add_call
end
end
- describe "mock created with no name" do
+ describe "double created with no name" do
it "does not use a name in a failure message" do
- mock = double()
- expect {mock.foo}.to raise_error(/Double received/)
+ double = double()
+ expect {double.foo}.to raise_error(/Double received/)
end
it "does respond to initially stubbed methods" do
@@ -647,22 +657,22 @@ def add_call
end
describe "with" do
- before { @mock = double('double') }
+ before { @double = double('double') }
context "with args" do
context "with matching args" do
it "passes" do
- @mock.should_receive(:foo).with('bar')
- @mock.foo('bar')
+ @double.should_receive(:foo).with('bar')
+ @double.foo('bar')
end
end
context "with non-matching args" do
it "fails" do
- @mock.should_receive(:foo).with('bar')
+ @double.should_receive(:foo).with('bar')
expect do
- @mock.foo('baz')
+ @double.foo('baz')
end.to raise_error
- @mock.rspec_reset
+ @double.rspec_reset
end
end
@@ -670,11 +680,11 @@ def add_call
it "fails" do
d1 = double('1')
d2 = double('2')
- @mock.should_receive(:foo).with(d1)
+ @double.should_receive(:foo).with(d1)
expect do
- @mock.foo(d2)
+ @double.foo(d2)
end.to raise_error
- @mock.rspec_reset
+ @double.rspec_reset
end
end
@@ -682,11 +692,11 @@ def add_call
it "fails" do
d1 = double('1').as_null_object
d2 = double('2').as_null_object
- @mock.should_receive(:foo).with(d1)
+ @double.should_receive(:foo).with(d1)
expect do
- @mock.foo(d2)
+ @double.foo(d2)
end.to raise_error
- @mock.rspec_reset
+ @double.rspec_reset
end
end
end
@@ -694,18 +704,18 @@ def add_call
context "with a block" do
context "with matching args" do
it "returns the result of the block" do
- @mock.should_receive(:foo).with('bar') { 'baz' }
- @mock.foo('bar').should eq('baz')
+ @double.should_receive(:foo).with('bar') { 'baz' }
+ @double.foo('bar').should eq('baz')
end
end
context "with non-matching args" do
it "fails" do
- @mock.should_receive(:foo).with('bar') { 'baz' }
+ @double.should_receive(:foo).with('bar') { 'baz' }
expect do
- @mock.foo('wrong').should eq('baz')
+ @double.foo('wrong').should eq('baz')
end.to raise_error(/received :foo with unexpected arguments/)
- @mock.rspec_reset
+ @double.rspec_reset
end
end
end