TDD often is well understood, but seldom put to good use. Spikes grow larger, hard to test aspects are skipped and sooner or later your test coverage looks like this.
.
Therefore i want to show you the Black-White-Tree testing method, which is easy to adopt and results in full C1(path) coverage with easy to maintain, independent tests.
.
.
The principle is simple:
When designing a new method build Black-Box tests for it, often 2-3 are sufficient if they exersice all paths within this method(not necessarily its sub-methods), represented by the trunk and the black branches.
describe :price do it "sums prices and applies discounts" do Order.new(:items=>items,:discount=>20).price.should == 22.5 end it "costs nothing if it is free" do Order.new(:items=>items,:free=>true,:discount=>10).price.should == 0 end end
Then write White Box tests, for the public method, mocking everything out with forged return values to verify that every method is called and the call-results are used logically.
describe :price do ... it "uses sum_price and apply_discount" do order = Order.new(:items=>items,:discount=>10) order.expects(:sum_prices).returns 100 order.expects(:apply_discount).with(10,100).returns 20 order.price.should == 20 end end
Then build the method, making all White Box and some of the Black Box tests pass.
Repeat for every sub-method.
Tagged: RSpec, TDD, Test
