Here you have Shoulda
macro for testing accepts_nested_attributes_for
: http://mediumexposure.com/testing-acceptsnestedattributesfor-shoulda-macros/. It does not support any options (such as :reject_if
), only bare accepts_nested_attributes_for
.
But for :reject_if
, you can create a valid Greeting model with nested attributes for User
but without :name
. Then check if user has been saved, and then same with blank :email
So you can do something like this:
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