Here's the schema of the model I want to test:
create_table "retreats", force: :cascade do |t|
t.string "title"
t.string "tagline"
t.string "type_of"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "description"
t.string "schedule"
t.boolean "available", default: true
end
Here's the Retreat model:
class Retreat < ApplicationRecord
TYPES_OF_RETREATS = ['individual', 'group']
validates :title, presence: true
validates :type_of, presence: true, inclusion: {in: TYPES_OF_RETREATS,
message: "%{value} is not a valid type."}
has_many :testimonials, dependent: :destroy
has_many :images, dependent: :destroy
has_and_belongs_to_many :dates, class_name: "RetreatDate", foreign_key:
'retreat_id', association_foreign_key: 'retreat_date_id'
end
These are the test cases I've written:
test "retreat should not save without a title" do
retreat = retreats(:no_title)
assert_not retreat.save, "Saved a retreat without a title"
end
test "retreat should not save without a type" do
retreat = retreats(:no_type)
assert_not retreat.save, "Saved a retreat without a type"
end
test "retreat can have a tagline, description, schedule and available" do
retreat = retreats(:all_attributes)
assert retreat.save, "Retreat failed to save"
end
test "retreat type should be from the provided list" do
retreat = retreats(:invalid_type)
assert_not retreat.save, "Some other retreat got saved. It shouldn't
have gotten saved."
end
test "retreat can have many testimonials" do
retreat = retreats(:one)
retreat.testimonials << Testimonial.new(statement: 'this is my
testimonial', participant_name: 'abc')
assert retreat.save, "Retreat did not save with the testimonials."
end
test "retreat can have many dates" do
retreat = retreats(:one)
retreat.dates.create({date: '02-08-2012'})
retreat.dates.create({date: '02-08-2013'})
assert retreat.save, "Retreat with multiple dates is not saving"
assert_equal(2, retreat.dates.length, "Retreat isn't saving multiple
dates.")
end
I am looking for advice on what kind of test cases should I write tests for. I feel that some of my test cases are unnecessary. Like test cases for validation make sense but testing if I can add multiple testimonials makes me uncomfortable.
What are the best practises for writing unit tests? And how can I write better unit tests?
Thanks :)
Aucun commentaire:
Enregistrer un commentaire