diff --git a/spec/example_app/app/models/customer.rb b/spec/example_app/app/models/customer.rb index a780e67da6..306a327f3a 100644 --- a/spec/example_app/app/models/customer.rb +++ b/spec/example_app/app/models/customer.rb @@ -1,5 +1,5 @@ class Customer < ActiveRecord::Base - has_many :orders + has_many :orders, dependent: :destroy validates :name, presence: true validates :email, presence: true diff --git a/spec/example_app/app/models/order.rb b/spec/example_app/app/models/order.rb index 327db20923..c572870b06 100644 --- a/spec/example_app/app/models/order.rb +++ b/spec/example_app/app/models/order.rb @@ -2,7 +2,7 @@ class Order < ActiveRecord::Base belongs_to :customer validates :customer, presence: true - has_many :line_items + has_many :line_items, dependent: :destroy validates :address_line_one, presence: true validates :address_line_two, presence: true diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb index a29f73d55a..873794cbed 100644 --- a/spec/models/customer_spec.rb +++ b/spec/models/customer_spec.rb @@ -6,6 +6,15 @@ it { should validate_presence_of(:name) } it { should validate_presence_of(:email) } + it "deletes associated orders when deleted itself" do + customer = create(:customer) + create(:order, customer: customer) + + puts customer.destroy + + expect(Order.all).to be_empty + end + describe "#lifetime_value" do it "returns 0 for no orders" do customer = Customer.new diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 7c04f870fc..62ada4434c 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -13,6 +13,17 @@ it { should validate_presence_of(:address_zip) } end + it "deletes associated line_items when deleted itself" do + product = create(:product) + customer = create(:customer) + order = create(:order, customer: customer) + create(:line_item, product: product, order: order) + + order.destroy + + expect(LineItem.all).to be_empty + end + describe "#total_price" do it "returns 0 when there are no line items" do order = build(:order)