Permalink
Browse files

Add verify_constant_names config option.

When this is set to true, an error will be raised if a given constant name is undefined.
  • Loading branch information...
1 parent 5e72da6 commit 4691ff77540a84cb796354dc63e4ad353327a483 @myronmarston myronmarston committed Jul 9, 2012
Showing with 57 additions and 0 deletions.
  1. +30 −0 lib/rspec/fire.rb
  2. +27 −0 spec/fire_double_spec.rb
View
@@ -4,6 +4,26 @@
module RSpec
module Fire
+ class Configuration
+ attr_accessor :verify_constant_names
+ alias verify_constant_names? verify_constant_names
+
+ def initialize
+ self.verify_constant_names = false
+ end
+ end
+
+ def self.configuration
+ @configuration ||= Configuration.new
+ end
+
+ def self.configure
+ yield configuration
+ end
+
+ Error = Class.new(StandardError)
+ UndefinedConstantError = Class.new(Error)
+
class SupportArityMatcher
def initialize(arity)
@arity = arity
@@ -179,11 +199,18 @@ def ensure_implemented(*method_names)
methods.sort.map {|x|
" #{x}"
}.join("\n")
+
]
raise RSpec::Expectations::ExpectationNotMetError, msg
end
end
end
+
+ def verify_constant_name
+ return if recursive_const_defined?(@__doubled_class_name)
+
+ raise UndefinedConstantError, "#{@__doubled_class_name} is not a defined constant."
+ end
end
class FireObjectDouble < RSpec::Mocks::Mock
@@ -193,6 +220,7 @@ def initialize(doubled_class, *args)
args << {} unless Hash === args.last
@__doubled_class_name = doubled_class
+ verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
# __declared_as copied from rspec/mocks definition of `double`
args.last[:__declared_as] = 'FireDouble'
@@ -213,6 +241,8 @@ def self.build(doubled_class, stubs = {})
@__checked_methods = :public_methods
@__method_finder = :method
+ verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
+
# TestDouble was added after rspec 2.9.0, and allows proper mocking
# of public methods that have clashing private methods. See spec for
# details.
@@ -29,6 +29,31 @@ def self.use
end
end
+shared_examples_for "verifying named constants" do |double_method|
+ def clear_config
+ RSpec::Fire.instance_variable_set(:@configuration, nil)
+ end
+
+ before(:each) { clear_config }
+ after(:all) { clear_config }
+
+ it "allows mispelled constants by default" do
+ double = send(double_method, "TestClas")
+ double.should_receive(:undefined_method)
+ double.undefined_method
+ end
+
+ it "raises an error when constants are mispelled and the appropriate config option is set" do
+ RSpec::Fire.configure do |c|
+ c.verify_constant_names = true
+ end
+
+ expect {
+ send(double_method, "TestClas")
+ }.to raise_error(/TestClas is not a defined constant/)
+ end
+end
+
shared_examples_for 'a fire-enhanced double method' do
describe 'doubled class is not loaded' do
let(:doubled_object) { fire_double("UnloadedObject") }
@@ -159,6 +184,7 @@ def fail_for_arguments(expected, actual)
let(:doubled_object) { fire_double("TestObject") }
it_should_behave_like 'a fire-enhanced double'
+ it_should_behave_like "verifying named constants", :fire_double
it 'allows stubs to be passed as a hash' do
double = fire_double("TestObject", :defined_method => 17)
@@ -170,6 +196,7 @@ def fail_for_arguments(expected, actual)
let(:doubled_object) { fire_class_double("TestClass") }
it_should_behave_like 'a fire-enhanced double'
+ it_should_behave_like "verifying named constants", :fire_class_double
it 'uses a module for the doubled object so that it supports nested constants like a real class' do
doubled_object.should be_a(Module)

0 comments on commit 4691ff7

Please sign in to comment.