Best practice: individual or combined tests?

Hi folks,

I’m pretty new to TDD. Is there a standard approach on this: Is it
better to write individual tests, or combine them? A specific example

  • if a model Foo validates the presence of both bar and baz, is it
    better to have a test each for bar and for baz, or to combine the
    validations into a combined bar/baz test?

Thanks!

acreadinglist wrote:

Hi folks,

I’m pretty new to TDD. Is there a standard approach on this: Is it
better to write individual tests, or combine them? A specific example

  • if a model Foo validates the presence of both bar and baz, is it
    better to have a test each for bar and for baz, or to combine the
    validations into a combined bar/baz test?

I’d test them individually. In general, you want each test pretty
atomic.

So for example, take a look at
http://github.com/marnen/quorum2/blob/master/spec/models/event_spec.rb
(the validation specs start at line 217). I’ve got separate specs for
each validation.

That code could use some DRYing up, though, and I do sometimes do things
like

['one', 'two', 'three'].each do |field|
  it "should require #{field}" do
    @model.send "#{field}=", nil
    @model.should_not be_valid
  end
end

but note that this still produces three separate specs. I’d feel wrong
if it didn’t.

Thanks!

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

acreadinglist wrote:

Hi folks,

I’m pretty new to TDD. Is there a standard approach on this: Is it
better to write individual tests, or combine them? A specific example

  • if a model Foo validates the presence of both bar and baz, is it
    better to have a test each for bar and for baz, or to combine the
    validations into a combined bar/baz test?

I’d test them individually. In general, you want each test pretty
atomic.

So for example, take a look at
http://github.com/marnen/quorum2/blob/master/spec/models/event_spec.rb
(the validation specs start at line 217).

Trust Github to be smart about this! The direct URL for that line (!)
is
http://github.com/marnen/quorum2/blob/master/spec/models/event_spec.rb#L217
.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs