我想在使用RSpec的RoR中测试创建一个自定义匹配器。具有多个参数的RSpec和自定义匹配器
define :be_accessible do |attributes|
attributes = attributes.is_a?(Array) ? attributes : [attributes]
attributes.each do |attribute|
match do |response|
response.class.accessible_attributes.include?(attribute)
end
description { "#{attribute} should be accessible" }
failure_message_for_should { "#{attribute} should be accessible" }
failure_message_for_should_not { "#{attribute} should not be accessible" }
end
end
我希望能够写出像在我的测试如下:
...
should be_accessible(:name, :surname, :description)
...
但与上述定义的匹配,我必须通过符号的阵列,而不是分隔符号逗号,否则测试只检查第一个符号。
任何想法?
下面是一个应该匹配你的第一个需要的答案:http://stackoverflow.com/a/4643289/582863。无论如何,我对你的意图很好奇......你只是想减少你的rspec测试文件中的行数,还是测试你的模型属性的复杂可访问性? – Saaman 2013-04-21 21:44:32
你提供的链接的问题是,这不是一个“常规”的def方法,所以我不能使用*。回答你的问题,我只是想减少我的rspec的线:) – 2013-04-21 21:51:44