One point I glossed over in my earlier post on encapsulation was how it makes unit testing easier. Using the same example as the other post, testing the encapsulated code is pretty simple:
1 2 3 4 |
def test_overdue_customers assert_equal [customers(:late), customers(:really_late)], Customer.find_all_overdue end |
With a test like this, and some comprehensive fixtures, you know that you’ll never send a warning to a customer who doesn’t deserve it.
One thing which can get a little trickier with encapsulated code, is creating an instance you wish to test. You find yourself writing 500 lines of set up code just to make one assertion. Thankfully when working with persistence frameworks like Active Record, you can skip that whole step by creating some test fixtures.
Besides our talk, my most valuable experience at RailsConf was talking with T...
Jamis Buck and I have been writing similar articles lately. We’ve both...