Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: rspec/rspec-mocks
...
head fork: rspec/rspec-mocks
  • 18 commits
  • 17 files changed
  • 0 commit comments
  • 3 contributors
Commits on Aug 31, 2010
Justin Ko justinko stub_chain.feature
- Closes #13.
f5f187e
David Chelimsky dchelimsky add some doc to stub_chain.feature fee8d4d
Myron Marston myronmarston Removed unused file.
The Macros module is also defined in spec_helper.rb.
c546573
Myron Marston myronmarston Use custom include_method matcher to deal with difference in ruby 1.8…
… vs 1.9.
347ee03
Commits on Sep 01, 2010
Myron Marston myronmarston Remove unused lib/rspec/mocks/extensions.rb file. 3b2b138
Commits on Sep 03, 2010
Myron Marston myronmarston Remove dependency on jeweler.
- Allows us to have an always-up-to-date gemspec.
- Use bundler's build/install/release rake tasks rather than jeweler's.
- One less development dependency.
- No need for a separate VERSION file.
9e03463
Commits on Sep 08, 2010
David Chelimsky dchelimsky exclude ruby-debug from 1.9.1 in Gemfile 80d5a2b
David Chelimsky dchelimsky fully qualify classname for ruby-1.9.1 537838f
Commits on Sep 11, 2010
David Chelimsky dchelimsky Fix regression that broke stub_chain with block format
- obj.stub_chain(:a,:b) { :c }
5f24e7d
David Chelimsky dchelimsky Fixed regression that broke hash format for stub_chain
- obj.stub_chain(:a, :b => :c)
2c23b86
David Chelimsky dchelimsky add history 3c467e9
David Chelimsky dchelimsky fix changelog link in History 9003244
Commits on Sep 12, 2010
David Chelimsky dchelimsky double.as_null_object always returns true for respond_to?
- Closes #16.
972deb5
David Chelimsky dchelimsky prep for beta.21 a58d60c
David Chelimsky dchelimsky bump version to 2.0.0.beta.21 56323d9
Commits on Sep 13, 2010
David Chelimsky dchelimsky When not as_null_object, respond_to? now behaves as expected. dae790f
David Chelimsky dchelimsky adjust history for beta.22 release f36ed16
David Chelimsky dchelimsky bump version to 2.0.0.beta.22 07bc53f
7 Gemfile
View
@@ -1,15 +1,16 @@
source "http://rubygems.org"
gem "rake"
-gem "jeweler"
gem "cucumber"
gem "aruba", ">= 0.2.0"
gem "autotest"
gem "rspec-mocks", :path => "."
gem "rspec-core", :path => "../rspec-core"
gem "rspec-expectations", :path => "../rspec-expectations"
-if RUBY_VERSION.to_s =~ /1.9/
+
+case RUBY_VERSION.to_s
+when '1.9.2'
gem "ruby-debug19"
-else
+when /^1.8/
gem "ruby-debug"
end
10 History.md
View
@@ -0,0 +1,10 @@
+## rspec-mocks release history (incomplete)
+
+### 2.0.0.beta.22 / 2010-09-12
+
+[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.0.0.beta.20...v2.0.0.beta.22)
+
+* Bug fixes
+ * fixed regression that broke obj.stub_chain(:a, :b => :c)
+ * fixed regression that broke obj.stub_chain(:a, :b) { :c }
+ * respond_to? always returns true when using as_null_object
29 Rakefile
View
@@ -1,36 +1,11 @@
require 'bundler'
Bundler.setup
+Bundler::GemHelper.install_tasks
require 'rake'
-require 'rspec/mocks/version'
require 'rspec/core/rake_task'
require 'cucumber/rake/task'
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "rspec-mocks"
- gem.version = RSpec::Mocks::Version::STRING
- gem.summary = "rspec-mocks-#{RSpec::Mocks::Version::STRING}"
- gem.description = "RSpec's 'test double' framework, with support for stubbing and mocking"
- gem.email = "dchelimsky@gmail.com;chad.humphries@gmail.com"
- gem.homepage = "http://github.com/rspec/mocks"
- gem.authors = ["David Chelimsky", "Chad Humphries"]
- gem.rubyforge_project = "rspec"
- gem.add_development_dependency 'rspec-core', RSpec::Mocks::Version::STRING
- gem.add_development_dependency 'rspec-expectations', RSpec::Mocks::Version::STRING
- end
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
-end
-
-namespace :gem do
- desc "push to gemcutter"
- task :push => :build do
- system "gem push pkg/rspec-mocks-#{RSpec::Mocks::Version::STRING}.gem"
- end
-end
-
RSpec::Core::RakeTask.new(:spec)
RSpec::Core::RakeTask.new(:rcov) do |spec|
@@ -64,4 +39,4 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
-task :default => [:check_dependencies, :spec, :cucumber]
+task :default => [:spec, :cucumber]
1  VERSION
View
@@ -1 +0,0 @@
-2.0.0.beta.20
44 features/stubs/stub_chain.feature
View
@@ -0,0 +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 representing methods" do
+ it "returns the correct value" do
+ subject.stub_chain(:one, :two, :three).and_return(: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 eq(:four)
+ end
+ end
+ end
+ """
+ When I run "rspec stub_chain_spec.rb"
+ Then the output should contain "2 examples, 0 failures"
0  lib/rspec/mocks/extensions.rb
View
No changes.
29 lib/rspec/mocks/methods.rb
View
@@ -34,19 +34,19 @@ def unstub(sym)
# == Examples
#
# Article.stub_chain("recent.published") { [Article.new] }
- def stub_chain(*chain)
- methods = chain.join('.').split('.')
- if methods.length > 1
- if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, methods[0].to_sym)
- methods.shift
- matching_stub.invoke.stub_chain(*methods)
+ def stub_chain(*chain, &blk)
+ chain, blk = format_chain(*chain, &blk)
+ if chain.length > 1
+ if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym)
+ chain.shift
+ matching_stub.invoke.stub_chain(*chain)
else
next_in_chain = Object.new
- stub(methods.shift) { next_in_chain }
- next_in_chain.stub_chain(*methods)
+ stub(chain.shift) { next_in_chain }
+ next_in_chain.stub_chain(*chain, &blk)
end
else
- stub(methods.shift)
+ stub(chain.shift, &blk)
end
end
@@ -79,6 +79,17 @@ def __mock_proxy
@mock_proxy ||= Proxy.new(self)
end
end
+
+ def format_chain(*chain, &blk)
+ if Hash === chain.last
+ hash = chain.pop
+ hash.each do |k,v|
+ chain << k
+ blk = lambda { v }
+ end
+ end
+ return chain.join('.').split('.'), blk
+ end
end
end
end
4 lib/rspec/mocks/mock.rb
View
@@ -34,6 +34,10 @@ def to_s
alias_method :to_str, :to_s
+ def respond_to?(sym, incl_private=false)
+ __mock_proxy.null_object? ? true : super
+ end
+
private
def method_missing(sym, *args, &block)
2  lib/rspec/mocks/proxy.rb
View
@@ -37,7 +37,7 @@ def initialize(object, name=nil, options={})
def null_object?
@null_object
end
-
+
# Tells the object to ignore any messages that aren't explicitly set as
# stubs or message expectations.
def as_null_object
2  lib/rspec/mocks/version.rb
View
@@ -1,7 +1,7 @@
module RSpec # :nodoc:
module Mocks # :nodoc:
module Version # :nodoc:
- STRING = File.readlines(File.expand_path('../../../../VERSION', __FILE__)).first
+ STRING = '2.0.0.beta.22'
end
end
end
182 rspec-mocks.gemspec
View
@@ -1,170 +1,28 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
+$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
+require "rspec/mocks/version"
Gem::Specification.new do |s|
- s.name = %q{rspec-mocks}
- s.version = "2.0.0.beta.20"
+ s.name = "rspec-mocks"
+ s.version = RSpec::Mocks::Version::STRING
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["David Chelimsky", "Chad Humphries"]
+ s.email = "dchelimsky@gmail.com;chad.humphries@gmail.com"
+ s.homepage = "http://github.com/rspec/rspec-mocks"
+ s.summary = "rspec-mocks-#{RSpec::Mocks::Version::STRING}"
+ s.description = "RSpec's 'test double' framework, with support for stubbing and mocking"
- s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
- s.authors = ["David Chelimsky", "Chad Humphries"]
- s.date = %q{2010-08-24}
- s.description = %q{RSpec's 'test double' framework, with support for stubbing and mocking}
- s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
- s.extra_rdoc_files = [
- "README.markdown"
- ]
- s.files = [
- ".autotest",
- ".document",
- ".gitignore",
- "Gemfile",
- "License.txt",
- "README.markdown",
- "Rakefile",
- "VERSION",
- "autotest/discover.rb",
- "cucumber.yml",
- "features/README.markdown",
- "features/configuration.feature",
- "features/mocks/block_local_expectations.feature",
- "features/mocks/warn_when_expectation_is_set_on_nil.feature",
- "features/stubs/stub_implementation.feature",
- "features/support/env.rb",
- "lib/rspec/mocks.rb",
- "lib/rspec/mocks/argument_expectation.rb",
- "lib/rspec/mocks/argument_matchers.rb",
- "lib/rspec/mocks/error_generator.rb",
- "lib/rspec/mocks/errors.rb",
- "lib/rspec/mocks/extensions.rb",
- "lib/rspec/mocks/extensions/instance_exec.rb",
- "lib/rspec/mocks/extensions/object.rb",
- "lib/rspec/mocks/framework.rb",
- "lib/rspec/mocks/message_expectation.rb",
- "lib/rspec/mocks/method_double.rb",
- "lib/rspec/mocks/methods.rb",
- "lib/rspec/mocks/mock.rb",
- "lib/rspec/mocks/order_group.rb",
- "lib/rspec/mocks/proxy.rb",
- "lib/rspec/mocks/space.rb",
- "lib/rspec/mocks/spec_methods.rb",
- "lib/rspec/mocks/version.rb",
- "lib/spec/mocks.rb",
- "rspec-mocks.gemspec",
- "spec/rspec/mocks/and_yield_spec.rb",
- "spec/rspec/mocks/any_number_of_times_spec.rb",
- "spec/rspec/mocks/argument_expectation_spec.rb",
- "spec/rspec/mocks/at_least_spec.rb",
- "spec/rspec/mocks/at_most_spec.rb",
- "spec/rspec/mocks/block_return_value_spec.rb",
- "spec/rspec/mocks/bug_report_10260_spec.rb",
- "spec/rspec/mocks/bug_report_10263_spec.rb",
- "spec/rspec/mocks/bug_report_11545_spec.rb",
- "spec/rspec/mocks/bug_report_15719_spec.rb",
- "spec/rspec/mocks/bug_report_496_spec.rb",
- "spec/rspec/mocks/bug_report_600_spec.rb",
- "spec/rspec/mocks/bug_report_7611_spec.rb",
- "spec/rspec/mocks/bug_report_7805_spec.rb",
- "spec/rspec/mocks/bug_report_8165_spec.rb",
- "spec/rspec/mocks/bug_report_8302_spec.rb",
- "spec/rspec/mocks/bug_report_830_spec.rb",
- "spec/rspec/mocks/bug_report_957_spec.rb",
- "spec/rspec/mocks/double_spec.rb",
- "spec/rspec/mocks/failing_argument_matchers_spec.rb",
- "spec/rspec/mocks/hash_including_matcher_spec.rb",
- "spec/rspec/mocks/hash_not_including_matcher_spec.rb",
- "spec/rspec/mocks/mock_ordering_spec.rb",
- "spec/rspec/mocks/mock_space_spec.rb",
- "spec/rspec/mocks/mock_spec.rb",
- "spec/rspec/mocks/multiple_return_value_spec.rb",
- "spec/rspec/mocks/nil_expectation_warning_spec.rb",
- "spec/rspec/mocks/null_object_mock_spec.rb",
- "spec/rspec/mocks/once_counts_spec.rb",
- "spec/rspec/mocks/options_hash_spec.rb",
- "spec/rspec/mocks/partial_mock_spec.rb",
- "spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb",
- "spec/rspec/mocks/passing_argument_matchers_spec.rb",
- "spec/rspec/mocks/precise_counts_spec.rb",
- "spec/rspec/mocks/record_messages_spec.rb",
- "spec/rspec/mocks/stash_spec.rb",
- "spec/rspec/mocks/stub_chain_spec.rb",
- "spec/rspec/mocks/stub_implementation_spec.rb",
- "spec/rspec/mocks/stub_spec.rb",
- "spec/rspec/mocks/stubbed_message_expectations_spec.rb",
- "spec/rspec/mocks/twice_counts_spec.rb",
- "spec/rspec/mocks_spec.rb",
- "spec/spec_helper.rb",
- "spec/support/macros.rb",
- "specs.watchr"
- ]
- s.homepage = %q{http://github.com/rspec/mocks}
- s.rdoc_options = ["--charset=UTF-8"]
- s.require_paths = ["lib"]
- s.rubyforge_project = %q{rspec}
- s.rubygems_version = %q{1.3.7}
- s.summary = %q{rspec-mocks-2.0.0.beta.20}
- s.test_files = [
- "spec/rspec/mocks/and_yield_spec.rb",
- "spec/rspec/mocks/any_number_of_times_spec.rb",
- "spec/rspec/mocks/argument_expectation_spec.rb",
- "spec/rspec/mocks/at_least_spec.rb",
- "spec/rspec/mocks/at_most_spec.rb",
- "spec/rspec/mocks/block_return_value_spec.rb",
- "spec/rspec/mocks/bug_report_10260_spec.rb",
- "spec/rspec/mocks/bug_report_10263_spec.rb",
- "spec/rspec/mocks/bug_report_11545_spec.rb",
- "spec/rspec/mocks/bug_report_15719_spec.rb",
- "spec/rspec/mocks/bug_report_496_spec.rb",
- "spec/rspec/mocks/bug_report_600_spec.rb",
- "spec/rspec/mocks/bug_report_7611_spec.rb",
- "spec/rspec/mocks/bug_report_7805_spec.rb",
- "spec/rspec/mocks/bug_report_8165_spec.rb",
- "spec/rspec/mocks/bug_report_8302_spec.rb",
- "spec/rspec/mocks/bug_report_830_spec.rb",
- "spec/rspec/mocks/bug_report_957_spec.rb",
- "spec/rspec/mocks/double_spec.rb",
- "spec/rspec/mocks/failing_argument_matchers_spec.rb",
- "spec/rspec/mocks/hash_including_matcher_spec.rb",
- "spec/rspec/mocks/hash_not_including_matcher_spec.rb",
- "spec/rspec/mocks/mock_ordering_spec.rb",
- "spec/rspec/mocks/mock_space_spec.rb",
- "spec/rspec/mocks/mock_spec.rb",
- "spec/rspec/mocks/multiple_return_value_spec.rb",
- "spec/rspec/mocks/nil_expectation_warning_spec.rb",
- "spec/rspec/mocks/null_object_mock_spec.rb",
- "spec/rspec/mocks/once_counts_spec.rb",
- "spec/rspec/mocks/options_hash_spec.rb",
- "spec/rspec/mocks/partial_mock_spec.rb",
- "spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb",
- "spec/rspec/mocks/passing_argument_matchers_spec.rb",
- "spec/rspec/mocks/precise_counts_spec.rb",
- "spec/rspec/mocks/record_messages_spec.rb",
- "spec/rspec/mocks/stash_spec.rb",
- "spec/rspec/mocks/stub_chain_spec.rb",
- "spec/rspec/mocks/stub_implementation_spec.rb",
- "spec/rspec/mocks/stub_spec.rb",
- "spec/rspec/mocks/stubbed_message_expectations_spec.rb",
- "spec/rspec/mocks/twice_counts_spec.rb",
- "spec/rspec/mocks_spec.rb",
- "spec/spec_helper.rb",
- "spec/support/macros.rb"
- ]
+ s.rubygems_version = "1.3.7"
+ s.rubyforge_project = "rspec"
- if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
- s.specification_version = 3
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.extra_rdoc_files = [ "README.markdown" ]
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_path = "lib"
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_development_dependency(%q<rspec-core>, ["= 2.0.0.beta.20"])
- s.add_development_dependency(%q<rspec-expectations>, ["= 2.0.0.beta.20"])
- else
- s.add_dependency(%q<rspec-core>, ["= 2.0.0.beta.20"])
- s.add_dependency(%q<rspec-expectations>, ["= 2.0.0.beta.20"])
- end
- else
- s.add_dependency(%q<rspec-core>, ["= 2.0.0.beta.20"])
- s.add_dependency(%q<rspec-expectations>, ["= 2.0.0.beta.20"])
- end
+ s.add_dependency "rspec-core", RSpec::Mocks::Version::STRING
+ s.add_dependency "rspec-expectations", RSpec::Mocks::Version::STRING
end
61 spec/rspec/mocks/null_object_mock_spec.rb
View
@@ -2,45 +2,57 @@
module RSpec
module Mocks
- describe "a mock acting as a NullObject" do
+ describe "a double _not_ acting as a null object" do
before(:each) do
- @mock = RSpec::Mocks::Mock.new("null_object").as_null_object
+ @double = double('non-null object')
+ end
+
+ it "says it does not respond to messages it doesn't understand" do
+ @double.should_not respond_to(:foo)
+ end
+
+ it "says it responds to messages it does understand" do
+ @double.stub(:foo)
+ @double.should respond_to(:foo)
+ end
+ end
+
+ describe "a double acting as a null object" do
+ before(:each) do
+ @double = double('null object').as_null_object
+ end
+
+ it "says it responds to everything" do
+ @double.should respond_to(:any_message_it_gets)
end
it "allows explicit expectation" do
- @mock.should_receive(:something)
- @mock.something
+ @double.should_receive(:something)
+ @double.something
end
it "fails verification when explicit exception not met" do
lambda do
- @mock.should_receive(:something)
- @mock.rspec_verify
+ @double.should_receive(:something)
+ @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError)
end
it "ignores unexpected methods" do
- @mock.random_call("a", "d", "c")
- @mock.rspec_verify
+ @double.random_call("a", "d", "c")
+ @double.rspec_verify
end
it "allows expected message with different args first" do
- @mock.should_receive(:message).with(:expected_arg)
- @mock.message(:unexpected_arg)
- @mock.message(:expected_arg)
+ @double.should_receive(:message).with(:expected_arg)
+ @double.message(:unexpected_arg)
+ @double.message(:expected_arg)
end
it "allows expected message with different args second" do
- @mock.should_receive(:message).with(:expected_arg)
- @mock.message(:expected_arg)
- @mock.message(:unexpected_arg)
- end
- end
-
- describe "#null_object?" do
- it "defaults to false" do
- obj = double('anything')
- obj.should_not be_null_object
+ @double.should_receive(:message).with(:expected_arg)
+ @double.message(:expected_arg)
+ @double.message(:unexpected_arg)
end
end
@@ -50,5 +62,12 @@ module Mocks
obj.should be_null_object
end
end
+
+ describe "#null_object?" do
+ it "defaults to false" do
+ obj = double('anything')
+ obj.should_not be_null_object
+ end
+ end
end
end
21 spec/rspec/mocks/partial_mock_spec.rb
View
@@ -134,34 +134,19 @@ def private_method; end
it 'keeps public methods public' do
@object.should_receive(:public_method)
- with_ruby('1.9') do
- @object.public_methods.should include(:public_method)
- end
- with_ruby('1.8') do
- @object.public_methods.should include('public_method')
- end
+ @object.public_methods.should include_method(:public_method)
@object.public_method
end
it 'keeps private methods private' do
@object.should_receive(:private_method)
- with_ruby('1.9') do
- @object.private_methods.should include(:private_method)
- end
- with_ruby('1.8') do
- @object.private_methods.should include('private_method')
- end
+ @object.private_methods.should include_method(:private_method)
@object.public_method
end
it 'keeps protected methods protected' do
@object.should_receive(:protected_method)
- with_ruby('1.9') do
- @object.protected_methods.should include(:protected_method)
- end
- with_ruby('1.8') do
- @object.protected_methods.should include('protected_method')
- end
+ @object.protected_methods.should include_method(:protected_method)
@object.public_method
end
79 spec/rspec/mocks/stub_chain_spec.rb
View
@@ -7,14 +7,81 @@ module Mocks
@subject = Object.new
end
- it "returns expected value from chaining only one method call" do
- @subject.stub_chain(:msg1).and_return(:return_value)
- @subject.msg1.should equal(:return_value)
+
+ context "with one method in chain" do
+ context "using and_return" do
+ it "returns expected value from chaining only one method call" do
+ @subject.stub_chain(:msg1).and_return(:return_value)
+ @subject.msg1.should equal(:return_value)
+ end
+ end
+
+ context "using a block" do
+ it "returns the correct value" do
+ @subject.stub_chain(:msg1) { :return_value }
+ @subject.msg1.should equal(:return_value)
+ end
+ end
+
+ context "using a hash" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain(:msg1 => :return_value)
+ @subject.msg1.should equal(:return_value)
+ end
+ end
+ end
+
+ context "with two methods in chain" do
+ context "using and_return" do
+ it "returns expected value from chaining two method calls" do
+ @subject.stub_chain(:msg1, :msg2).and_return(:return_value)
+ @subject.msg1.msg2.should equal(:return_value)
+ end
+ end
+
+ context "using a block" do
+ it "returns the correct value" do
+ @subject.stub_chain(:msg1, :msg2) { :return_value }
+ @subject.msg1.msg2.should equal(:return_value)
+ end
+ end
+
+ context "using a hash" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain(:msg1, :msg2 => :return_value)
+ @subject.msg1.msg2.should equal(:return_value)
+ end
+ end
end
- it "returns expected value from chaining two method calls" do
- @subject.stub_chain(:msg1, :msg2).and_return(:return_value)
- @subject.msg1.msg2.should equal(:return_value)
+ context "with four methods in chain" do
+ context "using and_return" do
+ it "returns expected value from chaining two method calls" do
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:return_value)
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
+
+ context "using a block" do
+ it "returns the correct value" do
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg4) { :return_value }
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
+
+ context "using a hash" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
+
+ context "using a hash with a string key" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain("msg1.msg2.msg3.msg4" => :return_value)
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
end
it "returns expected value from chaining four method calls" do
4 spec/rspec/mocks/stub_implementation_spec.rb
View
@@ -61,8 +61,8 @@ def obj.foo; :original; end
obj = Object.new
lambda do
obj.unstub(:foo)
- end.should raise_error(MockExpectationError)
+ end.should raise_error(RSpec::Mocks::MockExpectationError)
end
end
end
-end
+end
16 spec/spec_helper.rb
View
@@ -5,12 +5,7 @@
module Macros
def treats_method_missing_as_private(options = {:noop => true, :subject => nil})
it "has method_missing as private" do
- with_ruby 1.8 do
- self.class.describes.private_instance_methods.should include("method_missing")
- end
- with_ruby 1.9 do
- self.class.describes.private_instance_methods.should include(:method_missing)
- end
+ self.class.describes.private_instance_methods.should include_method(:method_missing)
end
it "does not respond_to? method_missing (because it's private)" do
@@ -32,11 +27,9 @@ def treats_method_missing_as_private(options = {:noop => true, :subject => nil})
end
end
-module RSpec
- module Matchers
- def with_ruby(version)
- yield if RUBY_VERSION =~ Regexp.compile("^#{version.to_s}")
- end
+RSpec::Matchers.define :include_method do |expected|
+ match do |actual|
+ actual.map { |m| m.to_s }.include?(expected.to_s)
end
end
@@ -44,6 +37,5 @@ def with_ruby(version)
config.mock_with :rspec
config.color_enabled = true
config.extend(Macros)
- config.include(RSpec::Matchers)
config.include(RSpec::Mocks::Methods)
end
29 spec/support/macros.rb
View
@@ -1,29 +0,0 @@
-module Macros
- def treats_method_missing_as_private(options = {:noop => true, :subject => nil})
- it "has method_missing as private" do
- with_ruby 1.8 do
- described_class.private_instance_methods.should include("method_missing")
- end
- with_ruby 1.9 do
- described_class.private_instance_methods.should include(:method_missing)
- end
- end
-
- it "does not respond_to? method_missing (because it's private)" do
- formatter = options[:subject] || described_class.new({ }, StringIO.new)
- formatter.should_not respond_to(:method_missing)
- end
-
- if options[:noop]
- it "should respond_to? all messages" do
- formatter = described_class.new({ }, StringIO.new)
- formatter.should respond_to(:just_about_anything)
- end
-
- it "should respond_to? anything, when given the private flag" do
- formatter = described_class.new({ }, StringIO.new)
- formatter.respond_to?(:method_missing, true).should be_true
- end
- end
- end
-end

No commit comments for this range

Something went wrong with that request. Please try again.