[英]Rspec Tests fail using custom model validation
我有这个自定义验证方法,确保你不能对自己的内容进行投票,并且它导致相当多的RSpec测试失败, undefined method 'user_id' for nil:NilClass
有没有办法重写自定义验证,或使用本机rails验证?
failing tests
12) Vote votable type
Failure/Error: @vote = FactoryGirl.create(:vote)
NoMethodError:
undefined method `user_id' for nil:NilClass
# ./app/models/vote.rb:18:in `ensure_not_author'
# ./spec/models/vote_spec.rb:5:in `block (2 levels) in <top (required)>'
validate :ensure_not_author
vote.rb
attr_accessible :value, :votable_id, :votable_type
belongs_to :votable, polymorphic: true
belongs_to :user
def ensure_not_author
votable = self.votable_type.downcase
errors.add(:user_id, "You can't vote on your own content.") if self.votable.user_id == self.user_id
end
如果有人需要更多的代码只是喊
看起来像self.votable
是零。 似乎这个测试应该从投票的角度来看待投票。 如果是这种情况,那么在工厂为您的可投票人创建投票并将测试移至您的可投票实体的套件中,因为您确实测试了投票人无法对自己的内容进行投票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.