context "can have 2 companies associated with it(v2)" do
it "should have an error when multiple companies can't belong to an industry " do
@company1 = Factory(:company)
@company2 = Factory(:company)
@industry = Factory(:industry)
@company1.industry = @industry
@company2.industry = @industry
@industry.should have(2).companies
end
end
Этот тест не пройден, и мне тяжело с ним.
15 других тестов в порядке.
Проблема в том, когда я пытаюсь использовать связанные объекты.
Мои модели:
class Company < ActiveRecord::Base
belongs_to :industry
validates_presence_of :name
validates_length_of :state, :is => 2, :allow_blank => true
validates_length_of :zip, :maximum => 30, :allow_blank => true
end
class Industry < ActiveRecord::Base
has_many :companies
validates_presence_of :name
validates_uniqueness_of :name
default_scope :order => "name asc"
end
Просто вставка самих записей работает нормально -
context "can have 2 companies associated with it" do
it "should have an error when multiple companies can't belong to an industry " do
lambda do
@company1 = Factory(:company)
@company2 = Factory(:company)
@industry = Factory(:industry)
@company1.industry = @industry
@company2.industry = @industry
end.should change(Company, :count).by(2)
end
end
кстати, верх моей спецификации:
require 'spec_helper'
describe Industry do
before(:each) do
@industry = Factory(:industry)
end
Я также прокомментировал
# config.use_transactional_fixtures = true
внизу spec/spec_helper.rb
но это не помогло