I am trying to check the attribute {: _destroy = "1"} on accepts_nested_attributes_for for my has_many association.
I have work for the has_one association, but I cannot figure out how to do this for has_many.
Seek advice! Thanks ^^ Ben
describe "nested attributes" do
describe "cell phone" do
it "accepts nested cell phone" do
contact = FactoryGirl.create(:contact)
contact.build_cell_phone(FactoryGirl.build(:cell_phone, :contact => contact).attributes)
lambda {
contact.save
}.should change(CellPhone, :count).by(1)
end
it "allows destroy attribute" do
contact = FactoryGirl.create(:contact)
cell_phone_attributes = FactoryGirl.build(:cell_phone, :contact => contact).attributes
contact.build_cell_phone(cell_phone_attributes)
contact.save
contact.cell_phone.should be_kind_of(CellPhone)
cell_phone_attributes.merge({:_destroy => "1"})
lambda {
contact.cell_phone_attributes = cell_phone_attributes
}.should change(CellPhone, :count).from(1).to(0)
end
it "rejects a cell phone with a blank number" do
contact = FactoryGirl.create(:contact)
contact.build_cell_phone(FactoryGirl.build(:cell_phone, :contact => contact, :number => "").attributes)
lambda {
contact.save
}.should_not change(CellPhone, :count)
end
it "rejects a cell phone with a blank cell provider" do
contact = FactoryGirl.create(:contact)
contact.build_cell_phone(FactoryGirl.build(:cell_phone, :contact => contact, :cell_provider_id => "").attributes)
lambda {
contact.save
}.should_not change(CellPhone, :count)
end
end
describe "jobs" do
it "accepts nested jobs" do
contact = FactoryGirl.create(:contact)
contact.jobs_attributes = 3.times.collect { FactoryGirl.build(:job, :contact => contact).attributes }
lambda {
contact.save
}.should change(Job, :count).by(3)
end
xit "allows destroy attribute" do
contact = FactoryGirl.create(:contact)
jobs_attributes = 3.times.collect { FactoryGirl.build(:job, :contact => contact).attributes }
contact.jobs_attributes = jobs_attributes
contact.save
contact.jobs.count.should == 3
jobs_attributes = jobs_attributes.each.collect { |hash| hash.merge({:_destroy => "1"}) }
lambda {
contact.jobs_attributes = jobs_attributes
contact.save
}.should change(Job, :count).from(3).to(0)
end
end
end
source
share