Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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: c42engineering/rspec-mocks
...
head fork: c42engineering/rspec-mocks
Checking mergeability… Don't worry, you can still create the pull request.
  • 8 commits
  • 14 files changed
  • 0 commit comments
  • 3 contributors
Commits on Mar 12, 2012
@dchelimsky dchelimsky align gemspec with other rspec libs 21d21ce
@dchelimsky dchelimsky bump to 2.9.0.rc2 06ce1ab
Commits on Mar 17, 2012
@dchelimsky dchelimsky bump to 2.9.0 e62d381
@dchelimsky dchelimsky dev: add a script to download ruby-debug-base19-0.11.26.gem and linec…
…ache19-0.5.13.gem to a local cache

[ci skip]
eb5da85
@dchelimsky dchelimsky only one ref to yard please c8a5ef5
Commits on Mar 21, 2012
@myronmarston myronmarston Extract mock logic into TestDouble module.
This allows objects to be treated as a pure test double without needing to subclass RSpec::Mocks::Mock.

Closes #117.
af3f296
Commits on Mar 28, 2012
@maeve maeve and_raise should support intentionally raising an ArgumentError 38ddf48
@myronmarston myronmarston Merge pull request #121 from maeve/fix-raising-argumenterrors
and_raise should support intentionally raising an ArgumentError
2478a16
View
4 Changelog.md
@@ -1,5 +1,5 @@
-### 2.9.0.rc1 / 2012-03-11
-[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.0...v2.9.0.rc1)
+### 2.9.0 / 2012-03-17
+[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.0...v2.9.0)
Enhancements
View
13 Gemfile-custom.sample
@@ -3,7 +3,7 @@ group :development do
gem "relish", "~> 0.5.0"
gem "guard-rspec", "0.5.0"
gem "growl", "1.0.3"
- gem "spork", "0.9.0.rc9"
+ gem "spork", "0.9.0"
platforms :mri_18, :jruby do
gem "rcov", "0.9.10"
@@ -20,9 +20,16 @@ group :development do
gem 'ruby-debug-base19', '0.11.25'
gem 'linecache19', '0.5.12'
when '1.9.3'
- # NOTE - see http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug
- # for help setting up ruby-debug19 on ruby 1.9.3
gem 'ruby-debug19', '0.11.6'
+ # NOTE - as of 2012-03-17 the following two gems have not been released,
+ # so if you see either of these errors when trying to install the bundle:
+ #
+ # Could not find gem 'ruby-debug-base19 (= 0.11.26) ruby' in the gems available on this machine.
+ # Could not find gem 'linecache19 (= 0.5.13) ruby' in the gems available on this machine.
+ #
+ # ... run 'script/download-ruby-debug-19-dependencies' and try again
+ #
+ # See http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug for more info.
gem 'ruby-debug-base19', '0.11.26'
gem 'linecache19', '0.5.13'
end
View
2  lib/rspec/mocks/error_generator.rb
@@ -64,7 +64,7 @@ def raise_wrong_arity_error(args_to_yield, arity)
def intro
if @name
"#{@declared_as} #{@name.inspect}"
- elsif Mock === @target
+ elsif TestDouble === @target
@declared_as
elsif Class === @target
"<#{@target.inspect} (class)>"
View
1  lib/rspec/mocks/framework.rb
@@ -7,6 +7,7 @@
require 'rspec/mocks/methods'
require 'rspec/mocks/argument_matchers'
require 'rspec/mocks/proxy'
+require 'rspec/mocks/test_double'
require 'rspec/mocks/mock'
require 'rspec/mocks/argument_expectation'
require 'rspec/mocks/message_expectation'
View
22 lib/rspec/mocks/message_expectation.rb
@@ -179,14 +179,7 @@ def invoke(*args, &block)
@order_group.handle_order_constraint self
begin
- begin
- raise(@exception_to_raise) unless @exception_to_raise.nil?
- rescue ArgumentError => e
- raise e.exception(<<-MESSAGE)
-'and_raise' can only accept an Exception class if an instance can be constructed with no arguments.
-#{@exception_to_raise.to_s}'s initialize method requires #{@exception_to_raise.instance_method(:initialize).arity} arguments, so you have to supply an instance instead.
-MESSAGE
- end
+ raise_exception unless @exception_to_raise.nil?
Kernel::throw(*@args_to_throw) unless @args_to_throw.empty?
default_return_val = if !@method_block.nil?
@@ -210,6 +203,19 @@ def invoke(*args, &block)
end
# @private
+ def raise_exception
+ if !@exception_to_raise.respond_to?(:instance_method) ||
+ @exception_to_raise.instance_method(:initialize).arity <= 0
+ raise(@exception_to_raise)
+ else
+ raise ArgumentError.new(<<-MESSAGE)
+'and_raise' can only accept an Exception class if an instance can be constructed with no arguments.
+#{@exception_to_raise.to_s}'s initialize method requires #{@exception_to_raise.instance_method(:initialize).arity} arguments, so you have to supply an instance instead.
+MESSAGE
+ end
+ end
+
+ # @private
def called_max_times?
@expected_received_count != :any && @expected_received_count > 0 &&
@actual_received_count >= @expected_received_count
View
2  lib/rspec/mocks/method_double.rb
@@ -27,7 +27,7 @@ def stubs
# @private
def visibility
- if Mock === @object
+ if TestDouble === @object
'public'
elsif object_singleton_class.private_method_defined?(@method_name)
'private'
View
2  lib/rspec/mocks/methods.rb
@@ -125,7 +125,7 @@ def rspec_reset
def __mock_proxy
@mock_proxy ||= begin
- mp = if Mock === self
+ mp = if TestDouble === self
Proxy.new(self, @name, @options)
else
Proxy.new(self)
View
76 lib/rspec/mocks/mock.rb
@@ -1,81 +1,7 @@
module RSpec
module Mocks
class Mock
- include Methods
-
- # Creates a new test double with a `name` (that will be used in error
- # messages only)
- def initialize(name=nil, stubs_and_options={})
- if name.is_a?(Hash) && stubs_and_options.empty?
- stubs_and_options = name
- @name = nil
- else
- @name = name
- end
- @options = extract_options(stubs_and_options)
- assign_stubs(stubs_and_options)
- end
-
- # This allows for comparing the mock to other objects that proxy such as
- # ActiveRecords belongs_to proxy objects. By making the other object run
- # the comparison, we're sure the call gets delegated to the proxy
- # target.
- def ==(other)
- other == __mock_proxy
- end
-
- # @private
- def inspect
- "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
- end
-
- # @private
- def to_s
- inspect.gsub('<','[').gsub('>',']')
- end
-
- alias_method :to_str, :to_s
-
- # @private
- def respond_to?(message, incl_private=false)
- __mock_proxy.null_object? && message != :to_ary ? true : super
- end
-
- private
-
- def method_missing(message, *args, &block)
- raise NoMethodError if message == :to_ary
- __mock_proxy.record_message_received(message, *args, &block)
- begin
- __mock_proxy.null_object? ? self : super
- rescue NameError
- __mock_proxy.raise_unexpected_message_error(message, *args)
- end
- end
-
- def extract_options(stubs_and_options)
- if stubs_and_options[:null_object]
- @null_object = stubs_and_options.delete(:null_object)
- RSpec.deprecate(%Q["double('name', :null_object => true)"], %Q["double('name').as_null_object"])
- end
- options = {}
- extract_option(stubs_and_options, options, :__declared_as, 'Mock')
- options
- end
-
- def extract_option(source, target, key, default=nil)
- if source[key]
- target[key] = source.delete(key)
- elsif default
- target[key] = default
- end
- end
-
- def assign_stubs(stubs)
- stubs.each_pair do |message, response|
- stub(message).and_return(response)
- end
- end
+ include TestDouble
end
end
end
View
102 lib/rspec/mocks/test_double.rb
@@ -0,0 +1,102 @@
+module RSpec
+ module Mocks
+ # Implements the methods needed for a pure test double. RSpec::Mocks::Mock
+ # includes this module, and it is provided for cases where you want a
+ # pure test double without subclassing RSpec::Mocks::Mock.
+ module TestDouble
+ include Methods
+
+ # Extends the TestDouble module onto the given object and
+ # initializes it as a test double.
+ #
+ # @example
+ #
+ # module = Module.new
+ # RSpec::Mocks::TestDouble.extend_onto(module, "MyMixin", :foo => "bar")
+ # module.foo #=> "bar"
+ def self.extend_onto(object, name=nil, stubs_and_options={})
+ object.extend self
+ object.send(:__initialize_as_test_double, name, stubs_and_options)
+ end
+
+ # Creates a new test double with a `name` (that will be used in error
+ # messages only)
+ def initialize(name=nil, stubs_and_options={})
+ __initialize_as_test_double(name, stubs_and_options)
+ end
+
+ # This allows for comparing the mock to other objects that proxy such as
+ # ActiveRecords belongs_to proxy objects. By making the other object run
+ # the comparison, we're sure the call gets delegated to the proxy
+ # target.
+ def ==(other)
+ other == __mock_proxy
+ end
+
+ # @private
+ def inspect
+ "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
+ end
+
+ # @private
+ def to_s
+ inspect.gsub('<','[').gsub('>',']')
+ end
+
+ alias_method :to_str, :to_s
+
+ # @private
+ def respond_to?(message, incl_private=false)
+ __mock_proxy.null_object? && message != :to_ary ? true : super
+ end
+
+ private
+
+ def __initialize_as_test_double(name=nil, stubs_and_options={})
+ if name.is_a?(Hash) && stubs_and_options.empty?
+ stubs_and_options = name
+ @name = nil
+ else
+ @name = name
+ end
+ @options = extract_options(stubs_and_options)
+ assign_stubs(stubs_and_options)
+ end
+
+ def method_missing(message, *args, &block)
+ raise NoMethodError if message == :to_ary
+ __mock_proxy.record_message_received(message, *args, &block)
+ begin
+ __mock_proxy.null_object? ? self : super
+ rescue NameError
+ __mock_proxy.raise_unexpected_message_error(message, *args)
+ end
+ end
+
+ def extract_options(stubs_and_options)
+ if stubs_and_options[:null_object]
+ @null_object = stubs_and_options.delete(:null_object)
+ RSpec.deprecate(%Q["double('name', :null_object => true)"], %Q["double('name').as_null_object"])
+ end
+ options = {}
+ extract_option(stubs_and_options, options, :__declared_as, 'Mock')
+ options
+ end
+
+ def extract_option(source, target, key, default=nil)
+ if source[key]
+ target[key] = source.delete(key)
+ elsif default
+ target[key] = default
+ end
+ end
+
+ def assign_stubs(stubs)
+ stubs.each_pair do |message, response|
+ stub(message).and_return(response)
+ end
+ end
+ end
+ end
+end
+
View
2  lib/rspec/mocks/version.rb
@@ -1,7 +1,7 @@
module RSpec
module Mocks
module Version
- STRING = '2.9.0.rc1'
+ STRING = '2.9.0'
end
end
end
View
3  rspec-mocks.gemspec
@@ -16,9 +16,8 @@ Gem::Specification.new do |s|
s.rubyforge_project = "rspec"
s.files = `git ls-files -- lib/*`.split("\n")
- s.files += ["License.txt"]
+ s.files += %w[README.md License.txt Changelog.md .yardopts .document]
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
- s.extra_rdoc_files = [ "README.md", "License.txt"]
s.rdoc_options = ["--charset=UTF-8"]
s.require_path = "lib"
View
4 script/download-ruby-debug-19-dependencies
@@ -0,0 +1,4 @@
+mkdir -p vendor/cache
+cd vendor/cache
+wget http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
+wget http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
View
8 spec/rspec/mocks/mock_spec.rb
@@ -217,6 +217,14 @@ def @mock.method_with_default_argument(arg={}); end
}.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)
+ lambda {
+ @mock.something
+ }.should raise_error(ArgumentError, "error message")
+ end
+
it "fails with helpful message if submitted Exception requires constructor arguments" do
class ErrorWithNonZeroArgConstructor < RuntimeError
def initialize(i_take_an_argument)
View
57 spec/rspec/mocks/test_double_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+module RSpec
+ module Mocks
+ describe TestDouble do
+ before(:all) do
+ Module.class_eval do
+ private
+ def use; end
+ end
+ end
+
+ after(:all) do
+ Module.class_eval do
+ undef use
+ end
+ end
+
+ it 'can be extended onto a module to make it a pure test double that can mock private methods' do
+ double = Module.new
+ double.stub(:use)
+ expect { double.use }.to raise_error(/private method `use' called/)
+
+ double = Module.new { TestDouble.extend_onto(self) }
+ double.should_receive(:use).and_return(:ok)
+ double.use.should be(:ok)
+ end
+
+ it 'sets the test double name when a name is passed' do
+ double = Module.new { TestDouble.extend_onto(self, "MyDouble") }
+ expect { double.foo }.to raise_error(/Mock "MyDouble" received/)
+ end
+
+ it 'stubs the methods passed in the stubs hash' do
+ double = Module.new do
+ TestDouble.extend_onto(self, "MyDouble", :a => 5, :b => 10)
+ end
+
+ double.a.should eq(5)
+ double.b.should eq(10)
+ end
+
+ it 'indicates what type of test double it is in error messages' do
+ double = Module.new do
+ TestDouble.extend_onto(self, "A", :__declared_as => "ModuleMock")
+ end
+ expect { double.foo }.to raise_error(/ModuleMock "A"/)
+ end
+
+ it 'is declared as a mock by default' do
+ double = Module.new { TestDouble.extend_onto(self) }
+ expect { double.foo }.to raise_error(/Mock received/)
+ end
+ end
+ end
+end
+

No commit comments for this range

Something went wrong with that request. Please try again.