Forked from bravoecho/dependency_injection_example.rb
Created
November 20, 2011 22:40
-
-
Save drKreso/1381080 to your computer and use it in GitHub Desktop.
Flexibility with and without Dependency Injection
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
| require 'minitest/autorun' | |
| class UsTaxCode | |
| def generate(id) | |
| "US-#{id}" | |
| end | |
| end | |
| class BrazilTaxCode | |
| def generate(id) | |
| "#{id}-BR" | |
| end | |
| end | |
| class Customer | |
| attr_accessor :id | |
| def initialize(id) | |
| self.id = id | |
| end | |
| def tax_code(tax_code_generator = UsTaxCode) | |
| tax_code_generator.new.generate(id) | |
| end | |
| end | |
| class WhimsicalTaxCode | |
| def generate(id) | |
| "****#{id.to_s.reverse}****" | |
| end | |
| end | |
| describe Customer do | |
| it "has an american tax code by default" do | |
| Customer.new(123456).tax_code.must_equal "US-123456" | |
| end | |
| it "can compute any type of tax code" do | |
| customer = Customer.new(123456) | |
| customer.tax_code(BrazilTaxCode).must_equal "123456-BR" | |
| customer.tax_code(UsTaxCode).must_equal "US-123456" | |
| customer.tax_code(WhimsicalTaxCode).must_equal "****654321****" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment