【发布时间】:2010-08-24 18:54:07
【问题描述】:
我有许多定义非常相似的类的 ruby 文件(a.rb、b.rb、c.rb)。 (他们应该测试相同)
我已经编写了一个单元测试来测试这些子类,并且我已经以编程方式为每个类生成了上下文(见下文)——我应该改为以编程方式创建整个测试类吗?如果是,为什么以及如何?
我正在使用shoulda 单元测试扩展,所以我的文件看起来像这样:
a.rb
class ItsA
def number
1123
end
end
b.rb
class ItsB
def number
6784
end
end
test_letters.rb
require 'rubygems'
require 'test/unit'
require 'shoulda'
class LettersTest < Test::Unit::TestCase
Dir.glob('letters/*.rb') do |letter|
context "The #{letter} letter file"
setup do
# Here I require the ruby file and allocate
# @theclass to be an instance of the class in the file.
# I'm actually testing JavaScript using Harmony, but
# putting those details in might complicate my question.
end
should "return a number" do
assert @theclass.number.is_a? Number
end
end
end
这可以很好地完成这项工作,但我是否应该做一些其他的游戏并自动创建LetterATest、LetterBTest 等?如果是这样,你会怎么做,为什么?
【问题讨论】:
-
您可以将方法抽象到一个模块中,测试一次,然后包含在类中。
标签: ruby unit-testing shoulda testunit