Created
March 12, 2013 17:35
-
-
Save cgarvis/5145073 to your computer and use it in GitHub Desktop.
Possible approach to a hexagonal gem with rspec test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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