我有许多定义非常相似的类的 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
这可以很好地完成这项工作,但是我应该自动做一些其他的 jiggerypokery 和 createLetterATest
等LetterBTest
吗?如果是这样,你会怎么做,为什么?