I can't for the life of me figure out what's going wrong here. I have a Client model and an Invoice model.
Client.rb
has_many :invoices, dependent: :destroy
Invoices.rb
belongs_to :client
I have the following client spec:
it "destroys its children upon destruction" do
i = FactoryGirl.create(:invoice) # As per the factory, the :client is the parent of :invoice and is automatically created with the :invoice factory
lambda {
i.client.destroy
}.should change(Invoice.all, :count).by(-1)
end
And here are my factories:
Client factory
FactoryGirl.define do
factory :client do
city "MyString"
end
end
Invoice factory
FactoryGirl.define do
factory :invoice do
association :client
gross_amount 3.14
end
end
If I do i = FactoryGirl.create(:invoice)
and afterwards i.client.destroy
manually in the console, the invoice is in fact destroyed. But for some reason, the test fails, giving me "count should have been changed by -1, but was changed by 0".
What am I doing wrong?