3
votes

I'm just starting to learn the practice of BDD / TDD (world rejoices, I know). One of the things that I struggle with at this point is which tests are actually worth writing. Let's take these set of tests which I started for a model called Sport:

Factory.define :sport do |f|
  f.name 'baseball'
end


require 'spec_helper'

describe Sport do

  before(:each) do
    @sport_unsaved = Factory.build(:sport)  # returns an unsaved object
    @sport_saved = Factory.create(:sport) # returns a saved object
  end

  # Schema testing.
  it { should have_db_column(:name).of_type(:string) }
  it { should have_db_column(:created_at).of_type(:datetime) }
  it { should have_db_column(:updated_at).of_type(:datetime) }

  # Index testing.

  # Associations testing.
  it { should have_many(:leagues) }

  # Validations testing.
  it 'should only accept unique names' do
    @sport_unsaved.should validate_uniqueness_of(:name)
  end

  it { should validate_presence_of(:name) }

  it 'should allow valid values for name' do
    Sport::NAMES.each do |v|
      should allow_value(v).for(:name)
    end
  end

  it 'should not allow invalid values for name' do
    %w(swimming Hockey).each do |v|
      should_not allow_value(v).for(:name)
    end
  end

  # Methods testing.

end

Few specific questions that I have:

  1. Is it worth testing that the association sport.leagues returns a non-blank value?
  2. How about a test that ensure the model is invalid if a name is not specified?
  3. How about a test to make sure that a valid record is created and doesn't have any validation errors?

I could go on. Ideally, there would be some hard and fast rules I could follow to guide my testing effort. But I am guessing this comes with experience and good ole' pragmatism. I've thought about reading through the source code of several gems such as the rails core to gain a better understanding of what's worth testing and what isn't.

Any advice you experienced testers out there could offer?

2

2 Answers

2
votes
  1. Not if you're only re-testing Rails behavior.
  2. Yes--it's part of model validation and a requirement, why not make sure the requirement is met?
  3. Testing your assumptions regarding the save process is a good idea, and if there are any lifecycle listeners/observers they may not be fired until the save.

The Rails core tests won't help you decide what's a good idea to test in an application.

0
votes
  • What should you test ? Anything you would not want to be broken
  • When to stop writing tests ? When fear transforms into boredom

So if 1,2,3 are defects if the specified behavior is not exhibited, then you should have tests on all 3.

From the code snippets, personally I'd refrain from checking DB implementation (which columns exist and their details). Reason: I'd want to be able to change that over time without having to break a bunch of tests and fix all of them. Tests should only break if the behavior is broken. If the way (implementation) in which you satisfy them changes, the tests should not break/need modifications.
Focus on the What over the How.

HTH