【发布时间】:2013-03-06 02:49:45
【问题描述】:
我正在使用 Rspec 和 Selenium 设置一些自动化测试,但在尝试以编程方式创建示例时遇到了一些问题。
我有一个包含多个项目的数组,需要根据数据的更改进行多次测试。
数组:
def get_array
@stuff = { thing1, thing2, thing3 }
end
简化版测试:
describe "My tests" do
context "First round" do
before { #do some stuff }
@my_array = get_array
@my_array.each do |k|
it "should pass the test" do
k.should pass_the_test
end
end
end
context "Second round" do
before { #do some other stuff }
@my_array = get_array
@my_array.each do |k|
it "should pass the test" do
k.should pass_the_test
end
end
end
end
在这个例子中还不错,但每次都必须调用@my_array = get_array 绝对不是 DRY。随着我添加更多测试和复杂性,这很快就会失控,所以我想知道我缺少什么更简单/更好的方法。
我已经尝试过共享上下文和我能找到的任何其他方法,但似乎没有任何效果。
【问题讨论】: