在Rails 3中使用RSpec测试嵌套属性上的_destroy属性

时间:2012-01-20 20:40:48

标签: ruby-on-rails-3 rspec

我正在尝试在has_many关联上的accepts_nested_attributes_for上测试{:_destroy =“1”}属性。

我已经将它用于has_one关联,但我无法弄清楚如何为has_many做这件事。

寻求建议!谢谢^^本

     describe "nested attributes" do

        describe "cell phone" do # the has_one association
          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 # the has_many association
          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 # TODO skipped test
            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
            # these are the old jobs_attributes, there is no jobs_attributes getter method on Contact!
            jobs_attributes = jobs_attributes.each.collect { |hash| hash.merge({:_destroy => "1"}) }
            lambda {
              # this doesn't work, the attributes are from before the jobs were created
              contact.jobs_attributes = jobs_attributes
              contact.save
            }.should change(Job, :count).from(3).to(0) # this fails, Job.count == 3 after the test
          end
        end
      end

1 个答案:

答案 0 :(得分:0)

您是否将:allow_destroy => true选项传递给accepts_nested_attributes_for

应该看起来像:

class Contact
  accepts_nested_attributes_for :jobs, :allow_destroy => true
end