Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This example is adapted from Dan Manges's blog, dcmanges.com
- class Wallet ; attr_accessor :credit_balance ; end
- class Moviegoer
- attr_accessor :wallet
- def initialize
- # ...setup wallet attribute with correct credit balance
- end
- end
- class MovieTheater
- def collect_money(moviegoer, amount)
- # VIOLATION OF DEMETER (see text)
- if moviegoer.wallet.credit_balance < amount
- raise InsufficientFundsError
- else
- moviegoer.wallet.credit_balance -= due_amount
- @collected_amount += due_amount
- end
- end
- end
- # Imagine testing the above code:
- describe MovieTheater do
- describe "collecting money" do
- it "should raise error if moviegoer can't pay" do
- # "Mock trainwreck" is a warning of a Demeter violation
- wallet = mock('wallet', :credit_balance => 5.00)
- moviegoer = mock('moviegoer', :wallet => wallet)
- lambda { @theater.collect_money(moviegoer, 10.00) }.
- should raise_error(...)
- end
- end
- end
Add Comment
Please, Sign In to add comment