Cześć,
Przeglądam różne przykłady testowania modelu i mam wątpliwości, czy nie dubluję testów.
Przykład:
class Contact < ActiveRecord::Base
validates :name, presence: true,
length: { in: 3..30 }
....
end
RSpec.describe Contact, type: :model do
let(:contact) { FactoryGirl.build :contact }
subject { contact }
....
it { should validate_presence_of(:name) }
it { should validate_length_of(:name).is_at_least(3).is_at_most(30) }
....
it "is invalid if NAME is blank (nil)" do
contact = FactoryGirl.build :contact, name: nil
contact.valid?
expect(contact.errors[:name]).to include(I18n.t("errors.messages.blank"))
end
....
it "is invalid if a NAME is to short (<3)" do
contact = FactoryGirl.build :contact, name: ""
contact.valid?
expect(contact.errors[:name]).to include(I18n.t("errors.messages.too_short", count: 1))
end
it "is invalid with to long NAME (> 30)" do
contact = FactoryGirl.build :contact, name: "ABC" * 11
contact.valid?
expect(contact.errors[:name]).to include(I18n.t("errors.messages.too_long", count: 30))
end
....
end
Mam do Was pytanie. Czy te trzy ostatnie testy, to nie jest de facto powielanie testów:
it { should validate_presence_of(:name) }
it { should validate_length_of(:name).is_at_least(3).is_at_most(30) }
?