ruby on rails 3 - how can I test accepts_nested_attributes_for with Rspec Rails3 -


i have model follows:

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? }  

how can rspec test this?

here have shoulda macro testing accepts_nested_attributes_for: http://mediumexposure.com/testing-acceptsnestedattributesfor-shoulda-macros/. not support options (such :reject_if), bare accepts_nested_attributes_for.

but :reject_if, can create valid greeting model nested attributes user without :name. check if user has been saved, , same blank :email

so can this:

describe greeting   { expect { factory(:greeting, :user_attributes => factory_attributes_for(:user)) }.to change(user, :count).by(1) }   { expect { factory(:greeting, :user_attributes => factory_attributes_for(:user, :name => '')) }.to_not change(user, :count) }   { expect { factory(:greeting, :user_attributes => factory.attributes_for(:user, :email => '')) }.to_not change(user, :count) } end 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -