ruby-on-rails-3 – 如何使用Rspec Rails3测试acceptance_nested
发布时间:2020-12-16 19:51:24 所属栏目:百科 来源:网络整理
导读:我有一个模型如下: class Greeting ActiveRecord::Base attr_accessible :headline,:icon,:content belongs_to :user accepts_nested_attributes_for :user,:reject_if = proc { |a| a[:name].blank? || a[:email].blank? } 我该如何进行Rspec测试? 解决方
我有一个模型如下:
class Greeting < ActiveRecord::Base attr_accessible :headline,:icon,:content belongs_to :user accepts_nested_attributes_for :user,:reject_if => proc { |a| a[:name].blank? || a[:email].blank? } 我该如何进行Rspec测试? 解决方法
在这里,您有Shoulda宏用于测试acceptance_nested_attributes_for:
http://mediumexposure.com/testing-acceptsnestedattributesfor-shoulda-macros/.它不支持任何选项(如:reject_if),只有bare acceptance_nested_attributes_for.
但是对于:reject_if,您可以为User创建一个带有嵌套属性的有效Greeting模型,但不包含:name.然后检查用户是否已保存,然后与空白:电子邮件一样 所以你可以这样做: describe Greeting it { expect { Factory(:greeting,:user_attributes => Factory_attributes_for(:user)) }.to change(User,:count).by(1) } it { expect { Factory(:greeting,:user_attributes => Factory_attributes_for(:user,:name => '')) }.to_not change(User,:count) } it { expect { Factory(:greeting,:user_attributes => Factory.attributes_for(:user,:email => '')) }.to_not change(User,:count) } end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |