Testing Rails model validations with RSpec, without testing AR itself
Lets as setup we have model User
:
class User < ActiveRecord::Base
validate :name, presence: true, uniqueness: { case_sensitive: false }, on: :create
validate :password, presence: true, format: { with: /\A[a-zA-z]*\z/ }
end
A see several ways to test this:
it { expect(user).to validate_presence_of(:name).on(:create) }
or
it do
user = User.create(name: '')
expect(user.errors[:name]).to be_present
end
My main question is which of the approaches is better and why? Can suggest me different approach?
Additional questions:
- How much should I test? As an example, I can write so many tests for the regex, but it will be hell for maintenance.
- How much you think will be full test coverage in this example?
Aucun commentaire:
Enregistrer un commentaire