[rspec-users] Varying test data

David Chelimsky dchelimsky at gmail.com
Fri Jan 11 05:48:57 EST 2008


On Jan 11, 2008 4:33 AM, Kerry Buckley <kerry at kerrybuckley.com> wrote:
> This isn't specific to RSpec, but is hopefully on-topic for this list.
>
> I like (especially when "ping pong pairing") to write a spec, then
> write the smallest amount of code I can to pass it (especially when
> "ping pong pairing"). Sometimes this means hard-coding a return value,
> which means another spec is needed to prove that the code is really
> behaving as it should. Trivial example:
>
> ----------
> describe Adder do
>    it "should add two numbers" do
>      Adder.add(2, 2).should == 4
>    end
> end
>
> class Adder
>    def add a, b
>      4
>    end
> end
> ----------
> describe Adder do
>    it "should add 2 and 2" do
>      Adder.add(2, 2).should == 4
>    end
>    it "should add 3 and 4" do
>      Adder.add(3, 4).should == 7
>    end
> end
>
> class Adder
>    def add a, b
>      a + b
>    end
> end
> ----------
>
> It doesn't seem right though to have all those duplicate specs. An
> alternative is to generate random test data, but I'm not really
> comfortable doing that because it means the tests aren't strictly
> repeatable. I guess this is more of a problem with classic state-based
> testing, but even using BDD you still have to test state at the leaf
> nodes.
>
> Does anyone have an opinion about whether this is a problem, and
> whether there's a clean way of dealing with it?

The approach you are taking (writing the second example) is called
triangulation. An other approach is to recognize in the first example
that there is duplication between the example and the code being
described. In this case, the number 4. From a duplication-removing
perspective, that is sufficient motivation to make the change during
the refactoring phase in THIS particular example.

If you were describing an object that behaved fundamentally
differently depending on input values, then you'd want the different
examples. In this case, returning the sum is not fundamentally
different in my view.

That said, if you were REALLY building a calculator and not relying on
the addition facilities of the language you were using, you would
naturally have additional examples to cover edge cases. Double digits,
for example.

You might also want to run this by the testdrivendevelopment list
(http://tech.groups.yahoo.com/group/testdrivendevelopment/).

Cheers,
David

>
> Thanks,
>
> Kerry
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list