【发布时间】:2013-04-21 20:32:01
【问题描述】:
我正在尝试使用 RSpec 为我在 RoR 中的测试创建自定义匹配器。
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)
...
但是使用上面定义的匹配器,我必须传递一个符号数组而不是用逗号分隔的符号,否则测试只检查第一个符号。
有什么想法吗?
【问题讨论】:
-
这是一个应该符合您的第一个需求的答案:stackoverflow.com/a/4643289/582863。无论如何,我很好奇你在这里的意图......你只是想减少 rspec 测试文件中的行数,还是你在模型属性上测试复杂的可访问性?
-
您提供的链接的问题是这不是“常规” def 方法,所以我不能使用 *.回答你的问题,我只是想减少我的 rspec 行:)
标签: ruby-on-rails ruby ruby-on-rails-3 rspec