【发布时间】:2011-03-23 15:47:03
【问题描述】:
在测试或测试驱动开发方面,我是个大菜鸟,所以我在测试我的代码时真的很吃力。造成这种情况的最大原因......
我有一个模型,Photo,其中包含字段“地点”、“城市”、“州”和“国家”。
我创建了一个方法“location”,它可以接受一个参数,并将返回最小形式、短形式或完整形式的位置。这是模型代码:
def location( form=:full )
usa = country.eql?('US') || country.eql?('United States')
country_name = Carmen::country_name( self.country )
if self.state.present?
state_name = Carmen::state_name( self.state )
end
case [form, usa]
when [:min, true] then ( self.city.blank? ? self.place : self.city )
when [:min, false] then ( self.city.blank? ? self.place : self.city )
when [:short, true] then [ ( self.city.blank? ? self.place : self.city ), state_name ].join(', ')
when [:short, false] then [ ( self.city.blank? ? self.place : self.city ), country_name ].join(', ')
when [:full, true] then [ self.place, self.city, state_name ].delete_if{|a| a.blank? }.join(', ')
when [:full, false] then [ self.place, self.city, country_name ].delete_if{|a| a.blank? }.join(', ')
else raise "Invalid location format"
end
end
如您所见,它相当干净简洁。现在,这是我的规范:
describe "location" do
before do
@us = Photo.new(:place => "Main St.", :city => "Barville", :state => "TX", :country => "US")
@uk = Photo.new(:place => "High St.", :city => "Barchester", :country => "GB")
@it = Photo.new( :place => "il Commune", :city => "Baria", :state => "AR", :country => "IT" )
end
context "minimum form" do
it "should return city or place for US Photos" do
@us.location(:min).should == "Barville"
@us.city = ""
@us.location(:min).should == "Main St."
end
it "should return city or place for Internationals without states" do
@uk.location(:min).should == "Barchester"
@uk.city = ""
@uk.location(:min).should == "High St."
end
it "should return city or place for Internationals with states" do
@it.location(:min).should == "Baria"
@it.city = ""
@it.location(:min).should == "il Commune"
end
end
context "short form" do
it "should return city,state or place,state for US photos" do
@us.location(:short).should == "Barville, Texas"
@us.city = ""
@us.location(:short).should == "Main St., Texas"
end
it "should return city,country or place,country for Internationals without states" do
@uk.location(:short).should == "Barchester, United Kingdom"
@uk.city = ""
@uk.location(:short).should == "High St., United Kingdom"
end
it "should return city,country or place,country for Internationals with states" do
@it.location(:short).should == "Baria, Italy"
@it.city = ""
@it.location(:short).should == "il Commune, Italy"
end
end
context "full form" do
context "US Photos" do
it "should return place, city, state" do
@us.location(:full).should == "Main St., Barville, Texas"
end
it "should return place, state if city is blank" do
@us.city = ""
@us.location(:full).should == "Main St., Texas"
end
it "should return city, state if place is blank" do
@us.place = ""
@us.location(:full).should == "Barville, Texas"
end
end
context "Internationals without states" do
it "should return place, city, state" do
@uk.location(:full).should == "High St., Barchester, United Kingdom"
end
it "should return place, state if city is blank" do
@uk.city = ""
@uk.location(:full).should == "High St., United Kingdom"
end
it "should return city, state if place is blank" do
@uk.place = ""
@uk.location(:full).should == "Barchester, United Kingdom"
end
end
end
context "Internationals with states" do
it "should return place, city, state" do
@it.location(:full).should == "il Commune, Baria, Italy"
end
it "should return place, state if city is blank" do
@it.city = ""
@it.location(:full).should == "il Commune, Italy"
end
it "should return city, state if place is blank" do
@it.place = ""
@it.location(:full).should == "Baria, Italy"
end
end
end
所以,98 行测试代码来测试 17 行模型代码。这对我来说似乎很疯狂。另外,在 RSpec 中测试它所花费的时间坦率地说比在控制台中测试它所花费的时间要多得多。
所以,我有两个问题:
- 当然有更好的方法来做到这一点。有人可以建议重构吗?
- 测试代码与模型代码的比例是否正常,如果是,为什么值得花时间?
谢谢!!
-- 编辑--
明确一点,我最感兴趣的是重构测试代码,而不是定位方法。
【问题讨论】:
标签: ruby-on-rails ruby testing refactoring rspec