0
我有一個模型在軌道有多個has_many,:dependent =>:破壞它的關係。Rspec測試多個has_many,依賴摧毀一個模型
class XYZ <ActiveRecord::Base
has_many :abc, :dependent => :destroy
has_many :def, :dependent => :destroy
has_many :ghi, :dependent => :destroy
.......
end
我RPEC試驗在XYZ控制器:
describe 'destroy' do
it 'should destroy all the entities in has_many' do
@xyz= FactoryGirl.create(:xyz)
@abc= FactoryGirl.create(:abc, :xyz=> @xyz)
@def= FactoryGirl.create(:def, :xyz=> @xyz)
@ghi= FactoryGirl.create(:ghi, :xyz=> @xyz)
expect { @xyz.destroy }.to change { ABC.count }.by(-1)
end
end
如何測試摧毀XYZ是否真的降低了車型ABC,防守和GHI同時計數?或者唯一的方法就是爲個別has_many編寫單獨的測試,依賴關係:destroy關係?