diff --git a/app/models/rental.rb b/app/models/rental.rb index e001ff37c..397b590d1 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,4 +1,8 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie + + validates :customer_id, presence: true + validates :movie_id, presence: true + validates :due_date, presence: true end diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index c55490b8d..e3ce77543 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -19,8 +19,9 @@ c = Customer.new(registered_at: "Date registered", address: "An address", city: "Seattle", state: "WA", postal_code: "zip", phone: "111111111", account_credit: 15.50) c.wont_be :valid? + c.errors.messages.must_include :name - # can't be saved because it isn't valid + # can't be saved because it isn't valid c.save Customer.count.must_equal start_count diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 0bea59f1c..4b9e9633b 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -1,5 +1,26 @@ require "test_helper" describe Rental do + describe "validations" do + it "can be created when all fields are given" do + cus_id = Customer.first.id + mov_id = Movie.first.id + r = Rental.new(customer_id: cus_id, movie_id: mov_id, due_date: Date.new(2017, 12, 1)) -end + r.must_be :valid? + end # can be created + + it "can't be created without a customer_id, movie_id, or due_date" do + start_count = Rental.count + r = Rental.new() + + r.wont_be :valid? + r.errors.messages.must_include :customer_id + r.errors.messages.must_include :movie_id + r.errors.messages.must_include :due_date + + r.save + Rental.count.must_equal start_count + end + end # validations +end # Rental