0
我想写一个规范是例子的数量,即“它”应该......“做”是在运行时确定的。我试图把“它”的方法在我自己的方法,这样我可以把它多次:如何在运行时向rspec添加示例?
def new_method(test)
it "#{test} should... " do
end
end
然而,“它”的方法不能从目前的规格::例子:: ExampleGroup ::子类实例。
我想写一个规范是例子的数量,即“它”应该......“做”是在运行时确定的。我试图把“它”的方法在我自己的方法,这样我可以把它多次:如何在运行时向rspec添加示例?
def new_method(test)
it "#{test} should... " do
end
end
然而,“它”的方法不能从目前的规格::例子:: ExampleGroup ::子类实例。
为了避免重复代码,有时我做这样的事情:
describe SomeOjbect do
%w(a b c d e f g).each do |val|
it "should have a value of #{val}" do
# ...
end
end
end
这将创建规范7个例。我想,如果你真的是使用的方法死心塌地,你可以做这样的事情:
def new_method(grp, test)
grp.instance_eval do
it "#{test} should..." do
# ...
end
end
end
describe SomeObject do
new_method(self, "a")
new_method(self, "b")
new_method(self, "c")
new_method(self, "d")
# ...
end
在这里,您通过self
,这是describe
块的范围,instance_eval
使您可以执行代码,如果你在那个区域,所以it
方法可用。
感谢您的快速响应。感谢这些例子。如果您在开发时知道需要运行多少个示例,那么它们就可以工作。不幸的是,就我而言,我需要在运行时确定这一点。 – Kerry 2010-09-14 16:33:30
那么我也描述了如何让你的'new_method'思想起作用。 – 2010-09-14 20:32:18