Skip to content

Commit

Permalink
Use the new RSpec::Mocks::TestDouble module if it is available.
Browse files Browse the repository at this point in the history
See the following links for the background on this.

* rspec/rspec-mocks#117
* rspec/rspec-mocks@af3f296
  • Loading branch information
myronmarston committed Mar 21, 2012
1 parent be8cf2a commit ae90190
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/rspec/fire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,17 @@ def self.build(doubled_class, stubs = {})
@__checked_methods = :public_methods
@__method_finder = :method

stubs.each do |message, response|
stub(message).and_return(response)
if defined?(::RSpec::Mocks::TestDouble)
::RSpec::Mocks::TestDouble.extend_onto self,
doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
else
stubs.each do |message, response|
stub(message).and_return(response)
end

def self.method_missing(name, *args)
__mock_proxy.raise_unexpected_message_error(name, *args)
end
end

def self.as_replaced_constant(options = {})
Expand All @@ -171,10 +180,6 @@ def self.inspect
def self.name
@__doubled_class_name
end

def self.method_missing(name, *args)
__mock_proxy.raise_unexpected_message_error(name, *args)
end
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/fire_double_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'spec_helper'

def use; end
private :use

TOP_LEVEL_VALUE_CONST = 7

RSpec::Mocks::Space.class_eval do
Expand Down Expand Up @@ -34,6 +37,10 @@ class Nested
class NestedEvenMore
end
end

def self.use
raise "Y U NO MOCK?"
end
end

shared_examples_for 'a fire-enhanced double method' do
Expand Down Expand Up @@ -178,6 +185,16 @@ def fail_for_arguments(expected, actual)
double.a.should eq(5)
double.b.should eq(8)
end

it 'allows private methods to be stubbed, just like on a normal test double (but unlike a partial mock)' do
mod = Module.new
mod.stub(:use)
expect { mod.use }.to raise_error(/private method `use' called/)

fire_double = fire_class_double("TestClass")
fire_double.stub(:use)
fire_double.use # should not raise an error
end if defined?(::RSpec::Mocks::TestDouble)
end

def reset_rspec_mocks
Expand Down

0 comments on commit ae90190

Please sign in to comment.