In my journey of learning Rails, I'm wondering if I should test model validations. If so, is this a correct way to do it?
require 'test_helper'
class ShippingAddressTest < ActiveSupport::TestCase
test "shipping addresses validations" do
s = ShippingAddress.create
assert_not s.valid?, "Address was valid without any data"
assert s.errors.include? :name
assert s.errors.include? :phone
assert s.errors.include? :address
assert s.errors.include? :user
assert s.errors.include? :city
s.name = "Some"
s.phone = "8296964737"
s.address = "Calle #1"
s.user_id = 1
# Test Inclusion of city
s.city = "Santo"
assert_not s.valid?, "Address was valid with incorrect city"
s.city = "Santo Domingo"
assert s.valid?, "Address not valid with everything filled"
end
end
I just found out while reading Testing Rails guide about this:
3.3 What to Include in Your Unit Tests
Ideally, you would like to include a test for everything which could possibly break. It's a good practice to have at least one test for each of your validations and at least one test for every method in your model.
In this case, what would be the correct way to add a test for a single validation?