Tutaj masz makro Shoulda
do testowania accepts_nested_attributes_for
: http://mediumexposure.com/testing-acceptsnestedattributesfor-shoulda-macros/. Nie obsługuje żadnych opcji (takich jak :reject_if
), tylko gołe accepts_nested_attributes_for
.
Ale dla :reject_if
można utworzyć poprawny model powitania z zagnieżdżonymi atrybutami dla User
, ale bez :name
. Następnie sprawdzić, czy użytkownik został zapisany, a następnie sam z pustą :email
Więc można zrobić coś takiego:
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
Tak, działa :) – AnkitG