From 430d1a940d3684d7c5bbff0e39ea7834af620261 Mon Sep 17 00:00:00 2001 From: Xavier Shay Date: Tue, 1 Oct 2013 18:01:51 -0700 Subject: [PATCH] Provide const_double alias for class_double. --- Changelog.md | 1 + .../verifying_doubles/class_doubles.feature | 9 +++++---- lib/rspec/mocks/example_methods.rb | 5 ++++- spec/rspec/mocks/verifying_double_spec.rb | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 2a38e2935..ead214ee3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -28,6 +28,7 @@ Enhancements: ported from `rspec-fire` (Xavier Shay). * `as_null_object` on a verifying double only responds to defined methods (Xavier Shay). +* Provide `const_double` as an alias to `class_double` (Xavier Shay). * Improved performance of double creation, particularly those with many attributes. (Xavier Shay) * Default value of `transfer_nested_constants` option for constant stubbing can diff --git a/features/verifying_doubles/class_doubles.feature b/features/verifying_doubles/class_doubles.feature index 6dc2cfdde..b3ba91989 100644 --- a/features/verifying_doubles/class_doubles.feature +++ b/features/verifying_doubles/class_doubles.feature @@ -1,11 +1,12 @@ Feature: Using a class double - `class_double` is provided as a complement to `instance_double`, with the - difference that it verifies class methods on the given class rather than - instance methods. + `class_double` and `const_double` are provided as complements to + `instance_double`, with the difference that they verify instance methods on + the actual object assigned to the constant rather than the instance methods + of the given class. For classes, this is equivalent to the class methods. In addition, it also provides a convenience method `as_stubbed_const` to - replace concrete classes with the defined double. See [mutating + replace concrete classes or constants with the defined double. See [mutating constants](../mutating-constants) for more details. Note: `class_double` can be used for modules as well. We chose to stick with diff --git a/lib/rspec/mocks/example_methods.rb b/lib/rspec/mocks/example_methods.rb index 640b486af..bf88f1a84 100644 --- a/lib/rspec/mocks/example_methods.rb +++ b/lib/rspec/mocks/example_methods.rb @@ -54,11 +54,14 @@ def instance_double(doubled_class, *args) # Constructs a test double against a specific class. If the given class # name has been loaded, only class methods defined on the class are # allowed to be stubbed. In all other ways it behaves like a - # [double](double). + # [double](double). This also works for stubbing constants, and is + # aliased as `const_double`.. def class_double(doubled_class, *args) declare_verifying_double(ClassVerifyingDouble, doubled_class, *args) end + alias_method :const_double, :class_double + # Disables warning messages about expectations being set on nil. # # By default warning messages are issued when expectations are set on diff --git a/spec/rspec/mocks/verifying_double_spec.rb b/spec/rspec/mocks/verifying_double_spec.rb index 48a41de1e..c25ff9119 100644 --- a/spec/rspec/mocks/verifying_double_spec.rb +++ b/spec/rspec/mocks/verifying_double_spec.rb @@ -3,6 +3,7 @@ class LoadedClass M = :m N = :n + INSTANCE = LoadedClass.new def defined_instance_method; end def self.defined_class_method; end @@ -203,6 +204,22 @@ def prevents(&block) end end + describe 'const doubles' do + it 'replaces a constant and verifies instances methods' do + o = const_double("LoadedClass::INSTANCE").as_stubbed_const + + expect(LoadedClass::INSTANCE).to eq(o) + + prevents { expect(o).to receive(:undefined_instance_method) } + prevents { expect(o).to receive(:defined_class_method) } + prevents { o.defined_instance_method } + + expect(o).to receive(:defined_instance_method) + o.defined_instance_method + end + end + + describe 'when verify_doubled_constant_names config option is set' do include_context "with isolated configuration"