Я только начал использовать rspec, и я хотел бы узнать ваше мнение об этой спецификации.
У меня 2 модели
class City < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
validates :department_id, :presence => true
belongs_to :department
end
и
class Department < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
has_many :cities
end
Я записал эти спецификации, чтобы удовлетворить утверждение валидации и отношения:
city_spec.rb
describe City do
before(:each) do
@city = Factory(:city)
end
describe "validation" do
it "valid" do
@city.should be_valid
@city.should have(:no).errors_on(:name)
@city.should have(:no).errors_on(:department_id)
end
it "has a unique name" do
c = Factory.build(:city, :name => @city.name)
c.should_not be_valid
c.name = 'unique'
c.should be_valid
# or via shoulda
c.should validate_uniqueness_of(:name)
end
it "belongs to department" do
c = Factory.build(:city, :department_id => nil)
c.should have(1).error_on(:department_id)
c.department_id = @city.department_id
c.should be_valid
c.should belong_to(:department)
end
end
end
department_spec.rb
describe Department do
before(:each) do
@department = Factory(:department)
end
describe "validation" do
it "has a name" do
d = Factory.build(:department, :name => nil)
d.should_not be_valid
d.should have(1).error_on(:name)
d.name = 'good name'
d.should be_valid
end
it "has a unique name" do
d = Factory.build(:department, :name => @department.name)
d.should_not be_valid
d.name = 'good name'
d.should be_valid
end
it "has many cities" do
d = Factory.build(:department)
c1 = Factory.build(:city)
c2 = Factory.build(:city)
d.cities << c1
d.cities << c2
d.cities.size.should == 2
d.cities.first.should == c1
d.cities.last.should == c2
# or via shoulda
d.should have_many(:cities)
end
end
end
вы видите, я использовал также musta драгоценный камень, вы думаете, этот подход правильный? Я написал слишком много тестов для этой функции?
Спасибо