Skip to content

Commit

Permalink
Merge branch 'myronmarston-test_double_module'
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Mar 23, 2012
2 parents c41caf9 + 0eaaacf commit f76b95d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rvm:
gemfile:
- spec/gemfiles/rspec25
- spec/gemfiles/rspec-latest
- spec/gemfiles/rspec-master
20 changes: 14 additions & 6 deletions lib/rspec/fire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,20 @@ def self.build(doubled_class, stubs = {})
@__checked_methods = :public_methods
@__method_finder = :method

stubs.each do |message, response|
stub(message).and_return(response)
# TestDouble was added after rspec 2.9.0, and allows proper mocking
# of public methods that have clashing private methods. See spec for
# details.
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 @@ -214,10 +226,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
6 changes: 6 additions & 0 deletions spec/gemfiles/rspec-master
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source :rubygems

gem 'rspec'
gem 'rspec-mocks', :git => 'git://github.com/rspec/rspec-mocks.git'

gemspec :path => '../../'

0 comments on commit f76b95d

Please sign in to comment.