我刚刚进入测试阶段,我想知道如何为RSpec规范编写步骤,这样我就可以重用很多功能,比如登录等。
发布于 2011-07-09 22:52:47
通常,测试应该彼此隔离;如果许多测试需要做相同的事情,这表明它们正在重复一些工作。但有时这是不可避免的--例如,您经常需要让登录用户方便地测试经过身份验证的内容。
特别是在Ruby测试的情况下,很可能有人已经编写了一个库来解决您想要的特定问题。例如,在正确测试操作之前,通常需要一些已存在的数据--这就是存在的原因。
如果您想要执行行为驱动的集成测试,该测试遍历用户实际执行的所有步骤,则应该使用。
如果希望跨不同位置重用方法,可以将共享代码放在spec/support中
# spec/support/consumable_helper.rb
module ConsumableHelper
def consume(consumable)
calories = consumable.om_nom_nom
end
end
RSpec.configure do |config|
config.include ConsumableHelper
end如果要在多个区域测试相同的行为,请使用shared_examples_for和it_behaves_like
shared_examples_for "a Consumable" do
it "should be delicious" do
subject.should be_delicious
end
it "should provide nutrition" do
subject.calories.should > 0
end
end
describe Fruit do
it_behaves_like "a Consumable"
end
describe Meat do
it_behaves_like "a Consumable"
endhttps://stackoverflow.com/questions/6635446
复制相似问题