我如何获得RSpec的共享示例,例如Ruby Test :: Unit中的行为? [英] How do i get RSpec's shared examples like behavior in Ruby Test::Unit?
问题描述
在RSpec中,是否存在类似于Test :: Unit测试中的shared_examples的插件/扩展?
Is there a plugin/extension similar to shared_examples in RSpec for Test::Unit tests?
推荐答案
如果您使用的是Rails(或仅使用active_support),请使用
If you are using rails (or just active_support), use a Concern
.
require 'active_support/concern'
module SharedTests
extend ActiveSupport::Concern
included do
# This way, test name can be a string :)
test 'banana banana banana' do
assert true
end
end
end
如果您不使用active_support,则只需使用Module#class_eval
.
If you're not using active_support, just use Module#class_eval
.
这项技术建立在 Andy H. 的答案上,他指出:
This technique builds on Andy H.'s answer, where he points out that:
Test :: Unit测试只是Ruby类,因此您可以使用代码重用的[常规技术]
Test::Unit tests are just Ruby classes, so you can use [normal techniques] of code reuse
但因为它允许使用 ActiveSupport::Testing::Declarative#test
它的优点是不会耗尽下划线:)
but because it enables the use of ActiveSupport::Testing::Declarative#test
it has the advantage of not wearing out your underscore key :)
这篇关于我如何获得RSpec的共享示例,例如Ruby Test :: Unit中的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!