Skip to content

Instantly share code, notes, and snippets.

@cgarvis
Created March 12, 2013 17:35
Show Gist options
  • Save cgarvis/5145073 to your computer and use it in GitHub Desktop.
Save cgarvis/5145073 to your computer and use it in GitHub Desktop.
Possible approach to a hexagonal gem with rspec test
module HexagonalGem
class CreateCustomer
attr_reader :listener, :validator
def self.create customer, listener = {}
c = CreateCustomer.new(listener)
c.create(customer)
end
def initialize(listener)
@listener = listener
end
def create(customer)
HexagonalGem.repository.insert(customer)
@listener.created_customer(customer)
end
end
end
require 'spec_helper'
describe HexagonalGem::CreateCustomer do
let(:listener) { double('listener') }
let(:repo) { HexagonalGem.repository }
subject(:create_customer) { HexagonalGem::CreateCustomer.new(listener) }
context 'with valid data' do
let(:customer) { {email: '[email protected]'} }
it 'callbacks with success' do
listener.should_receive('created_customer')
create_customer.create(customer)
end
it 'saves to repo' do
listener.should_receive('created_customer')
expect{create_customer.create(customer)}.to change{repo.count}.from(0).to(1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment