Skip to content

Instantly share code, notes, and snippets.

@wrozka
Last active December 20, 2015 03:09
Show Gist options
  • Save wrozka/6061571 to your computer and use it in GitHub Desktop.
Save wrozka/6061571 to your computer and use it in GitHub Desktop.
rspec-mocks api on top of bogus.
require 'bogus/rspec'
class CallBuilder
attr_accessor :fake
def initialize(method, any_args, block, fake = nil)
@method = method
@fake = fake
@args = any_args
@block = block
build
end
def with(*args, &block)
@args = args
@block = block
build
end
def and_return(value)
@block = proc { value }
build
end
def build
fake.__send__(@method, *@args, &@block) if fake
self
end
end
class AllowBuilder
def initialize(fake)
@fake = fake
end
def to(builder)
builder.fake = @fake
builder.build
end
end
def double(name = nil)
fake = fake(name)
define_dsl = lambda do |dsl_method, wrapped_fake|
any_args = self.any_args
fake.define_singleton_method(dsl_method) do |params, &block|
if params.is_a?(Hash)
definition = params.first
method = definition.first
block = proc { definition.last }
else
method = params
end
CallBuilder.new(method, any_args, block, wrapped_fake)
end
end
define_dsl.call(:stub, stub(fake))
define_dsl.call(:should_receive, mock(fake))
fake
end
def allow(fake)
AllowBuilder.new(stub(fake))
end
def receive(method, &block)
CallBuilder.new(method, any_args, block)
end
class Order
def process(id)
end
end
describe "stubbing" do
it "stubs" do
order = double(:order)
order.stub(:process).with(1) { :ok }
order.process(1).should == :ok
order.process(2).should_not == :ok
end
it "stubs with a hash" do
order = double(:order)
order.stub(process: :ok)
order.process(1).should == :ok
order.process(2).should == :ok
end
it "stubs with and_return" do
order = double
order.stub(:process).with(1).and_return(:ok)
order.process(1).should == :ok
order.process(2).should_not == :ok
end
it "stubs without arguments" do
order = double(:order)
order.stub(:process).and_return(:ok)
order.process(1).should == :ok
order.process(2).should == :ok
end
it "allows" do
order = double
allow(order).to receive(:process).with(1) { :ok }
order.process(1).should == :ok
order.process(2).should_not == :ok
end
it "allows with and_return" do
order = double
allow(order).to receive(:process).with(1).and_return(:ok)
order.process(1).should == :ok
order.process(2).should_not == :ok
end
it "allows without arguments" do
order = double
allow(order).to receive(:process) { :ok }
order.process(1).should == :ok
order.process(2).should == :ok
end
it "should_receive" do
order = double
order.should_receive(:process).with(1) { :ok }
order.process(1).should == :ok
order.process(2).should_not == :ok
end
it "should_receive with a hash" do
order = double
order.should_receive(process: :ok)
order.process(1).should == :ok
order.process(2).should == :ok
end
it "should_receive with and_return" do
order = double
order.should_receive(:process).with(1).and_return(:ok)
order.process(1).should == :ok
order.process(2).should_not == :ok
end
it "should_receive without arguments" do
order = double
order.should_receive(:process) { :ok }
order.process(1).should == :ok
order.process(2).should == :ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment