Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/rental.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion test/models/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 22 additions & 1 deletion test/models/rental_test.rb
Original file line number Diff line number Diff line change
@@ -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