Skip to content

Commit

Permalink
#better_stub will take a hash or a symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
se3000 committed Jan 28, 2013
1 parent 7e958a9 commit e53f70f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 50 deletions.
18 changes: 14 additions & 4 deletions lib/better_receive/stub.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
module BetterReceive
class Stub < Base

def assert_with(selector, options={}, &block)
def assert_with(selector_or_hash, options={}, &block)
if selector_or_hash.is_a?(Hash)
selector_or_hash.each do |selector, value|
better_stub_method(selector, options, &block).and_return(value)
end
else
better_stub_method(selector_or_hash, options, &block)
end
end


private

def better_stub_method(selector, options, &block)
if subject_is_any_instance?
any_instance_better_expect(selector, options, &block)
else
Expand All @@ -10,9 +23,6 @@ def assert_with(selector, options={}, &block)
end
end


private

def stub_subject_method(selector, options, &block)
location = options[:expected_from] || caller(1)[2]
subject_mock_proxy.add_stub(location, selector, options, &block)
Expand Down
132 changes: 86 additions & 46 deletions spec/lib/better_receive/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,116 @@ def bar(baz = nil)
let(:foo) { Foo.new }
let(:br_stub) { BetterReceive::Stub.new(foo) }

context "when checking responds to" do
it "determines whether an object responds to a method" do
foo.should_receive(:respond_to?).with(:bar).and_return(true)
context "when passed a single selector" do
context "when checking responds to" do
it "determines whether an object responds to a method" do
foo.should_receive(:respond_to?).with(:bar).and_return(true)

br_stub.assert_with :bar
br_stub.assert_with :bar

foo.bar
end
foo.bar
end

it "raises an error if the method is not defined" do
expect {
br_stub.assert_with :bar_baz
}.to raise_error(RSpec::Expectations::ExpectationNotMetError) { |error|
error.message.should =~ /to respond to :bar_baz/
}
it "raises an error if the method is not defined" do
expect {
br_stub.assert_with :bar_baz
}.to raise_error(RSpec::Expectations::ExpectationNotMetError) { |error|
error.message.should =~ /to respond to :bar_baz/
}
end
end
end

context "when mocking" do
let(:mock_proxy) { double(RSpec::Mocks::Proxy).as_null_object }
context "when mocking" do
context "given a single selector"
let(:mock_proxy) { double(RSpec::Mocks::Proxy).as_null_object }

it "creates a mock proxy and adds an expectation to it" do
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
mock_proxy.should_receive(:add_stub)
it "creates a mock proxy and adds an expectation to it" do
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
mock_proxy.should_receive(:add_stub)

br_stub.assert_with :bar
end
br_stub.assert_with :bar
end

it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
br_stub.assert_with(:bar).should be_a RSpec::Mocks::MessageExpectation
it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
br_stub.assert_with(:bar).should be_a RSpec::Mocks::MessageExpectation


br_stub.assert_with(:bar).with('wibble')
end
br_stub.assert_with(:bar).with('wibble')
end

context "and passing arguments" do
let(:block_param) { Proc.new {} }
let(:options) { {passed: true} }
context "and passing arguments" do
let(:block_param) { Proc.new {} }
let(:options) { {passed: true} }

it "passes all arguments through to the mock_proxy" do
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
mock_proxy.should_receive(:add_stub) do |*args, &block|
it "passes all arguments through to the mock_proxy" do
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
mock_proxy.should_receive(:add_stub) do |*args, &block|
args[1].should == :bar
args[2].should == options
block.should == block_param
end

br_stub.assert_with(:bar, passed: true, &block_param)
end
end
end
end

br_stub.assert_with(:bar, passed: true, &block_param)
context "when passed a hash" do
class Foo
def extra; end
end

context "and checking responds to" do
it "determines whether an object responds to a method" do
params = {bar: '1', extra: '2'}

foo.should_receive(:respond_to?).with(:bar).and_return(true)
foo.should_receive(:respond_to?).with(:extra).and_return(true)

br_stub.assert_with params
end

it "raises an error if the method is not defined" do
params = {bar: '1', baz: '2'}

expect{
br_stub.assert_with params
}.to raise_error(RSpec::Expectations::ExpectationNotMetError) { |error|
error.message.should =~ /to respond to :baz/
}
end
end

context "and stubbing" do
it 'stubs out each method' do
params = {bar: '1', extra: '2'}
br_stub.assert_with params

foo.bar.should == '1'
foo.extra.should == '2'
end
end
end

context "on .any_instance" do
let(:br_stub) { BetterReceive::Stub.new(Foo.any_instance) }
context "on .any_instance" do
let(:br_stub) { BetterReceive::Stub.new(Foo.any_instance) }

context "when the method is defined" do
it 'stubs the method out' do
br_stub.assert_with(:bar).and_return(:whatever)
context "when the method is defined" do
it 'stubs the method out' do
br_stub.assert_with(:bar).and_return(:whatever)

foo.bar.should == :whatever
end
foo.bar.should == :whatever
end
end

context 'when the method is not defined' do
it 'raises an error' do
expect {
br_stub.assert_with(:baz)
}.to raise_error { |error|
error.message.should == "Expected instances of Foo to respond to :baz"
}
end
context 'when the method is not defined' do
it 'raises an error' do
expect {
br_stub.assert_with(:baz)
}.to raise_error { |error|
error.message.should == "Expected instances of Foo to respond to :baz"
}
end
end
end
Expand Down

0 comments on commit e53f70f

Please sign in to comment.