Last active
August 29, 2015 13:57
-
-
Save tygern/dadc41afe95c9b331452 to your computer and use it in GitHub Desktop.
External service test environment
This file contains hidden or 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
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