2016-06-14 28 views
0

validate_presence_of使用早该/ RSpec的我得到这些长期和重复的代码块多属性测试喜欢你的:RSpec的/早该validate_presence_of多个属性语法

it { should validate_presence_of(:text) } 
it { should validate_presence_of(:user) } 
it { should validate_presence_of(:commentable) } 
[...] 

有没有办法变干这个吗?这样的事情:

it { should validate_presence_of(:text, :user, :commentable,...) } 

回答

1

据我所知,没有什么内置的应该为此。通常,您会希望将选项链接到应用宏,例如.with_message(...),所以你的语法建议将不可能用于这些情况。

你可以代替这样做:

[:text, :user, :commentable].each do |field| it { should validate_presence_of(field) } end

不过,我不会太担心有重复的一点点在你的测试套件,方便阅读的目的和维护。