我刚开始学习BDD/TDD实践(众人欢呼,知道的我也很高兴)。目前我遇到的一个问题是不知道到底哪些测试值得编写。我们以我为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
我有几个具体问题:
- 测试关联 sport.leagues 返回的是否为空值是否值得?
- 如何测试确保模型无效,如果没有指定名称?
- 如何测试确保创建有效记录且没有任何验证错误?
有什么建议吗?在座有经验的测试人员能提供吗?