Skip to content

Instantly share code, notes, and snippets.

@tygern
Last active August 29, 2015 13:57
Show Gist options
  • Save tygern/dadc41afe95c9b331452 to your computer and use it in GitHub Desktop.
Save tygern/dadc41afe95c9b331452 to your computer and use it in GitHub Desktop.
External service test environment
class ExternalEnvironment
def initialize
@account_ids = []
yield self
ensure
clean_up
end
def create_account(data)
account = External::Account.create(data)
if account.errors.empty?
@account_ids << account.id
else
raise "Failed to create account due to #{account.errors.inspect}"
end
account
end
private
def clean_up
delete_accounts
end
def delete_accounts
failed_deletions = []
@account_ids.each do |id|
success = External::Account.delete(id)
failed_deletions << id unless success
end
raise "Failed to delete accounts #{failed_deletions.inspect}" unless failed_deletions.empty?
end
end
describe "Some feature" do
it "behaves as expected" do
ExternalEnvironment.new do |service|
account = service.create_account(name: "Peter", age: 36)
expect(account.name).to eq "Peter"
expect(account.age).to eq 36
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment