On 4/3/13 2:53 AM, Jacob Carlborg wrote:
On 2013-04-03 05:03, Jonathan M Davis wrote: I actually prefer to have repetitive unit tests and not using loops to make it clear what they actually do. Here's an example from our code base, in Ruby:describe "Swedish" do subject { build(:address) { |a| a.country_id = Country::SWEDEN } } it { should validate_postal_code(12345) } it { should validate_postal_code(85412) } it { should_not validate_postal_code(123) } it { should_not validate_postal_code(123456) } it { should_not validate_postal_code("05412") } it { should_not validate_postal_code("fooba") } end describe "Finnish" do subject { build(:address) { |a| a.country_id = Country::FINLAND } } it { should validate_postal_code(12345) } it { should validate_postal_code(12354) } it { should validate_postal_code(41588) } it { should validate_postal_code("00123") } it { should validate_postal_code("01588") } it { should validate_postal_code("00000") } it { should_not validate_postal_code(1234) } it { should_not validate_postal_code(123456) } it { should_not validate_postal_code("fooba") } end It could be written less repetitive, like this: postal_codes = { Country::SWEDEN => { valid: [12345, 85412], invalid: [123, 123456, "05412", "fooba"] }, Country::FINLAND => { valid: [12345, 12354, 41588], invalid: ["00123", "01588", "00000", 1234, 123456, "fooba"] } } postal_codes.each do |country_id, postal_codes| describe c.english_name do subject { build(:address) { |a| a.country_id = country_id } } postal_codes[:valid].each do |postal_code| it { should validate_postal_code(postal_code) } end postal_codes[:invalid].each do |postal_code| it { should_not validate_postal_code(postal_code) } end end end But I don't think that looks any better. I think it's much worse.
The way I see it, the first is terrible and the second asks for better focus on a data-driven approach.
Andrei
