You are currently browsing the tag archive for the 'TDD' tag.
On my new project we had the problem that some specs failed when ran on their own, and some specs produces strange output (like “use object_id”), so I build a helper that runs each test on its own and could pinpoint the problem.
rake spec:one_by_one
it is now included in the single test rails plugin
Autotest until recently only had one flaw: it could not be used for large test suites, since after each red-green cycle I had to wait x minutes for all tests to pass, which made autotest really frustrating.
So grep autotest (the ‘without ZenTest version’) from github and enjoy “autotest -c” (also works with auospec).
And remember kids: always run autospec with script/spec_server and its twice the fun
All of the sudden some columns began to misbehave in cucumber tests, claiming that “Attempt to call private method (NoMethodError)” on normal column attributes!
A simple fix for this, essentially saying that a method is not private when its a column:
#features/support/ar_private_fix.rb required from env.rb
unless ActiveRecord::Base.methods.include?('private_method_defined_with_fix?')
class ActiveRecord::Base
class << self
def private_method_defined_with_fix?(method)
return false if columns_hash.keys.include?(method.to_s)
private_method_defined_without_fix?(method)
end
alias_method_chain :private_method_defined?, :fix
end
end
end
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.


