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
This test is failing and I am having a hard time with it.
15 other tests are ok.
The problem is when I try to use related objects.
My models are:
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
Just inserting the records themselves does seem to work ok –
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
btw the top of my spec is:
require 'spec_helper'
describe Industry do
before(:each) do
@industry = Factory(:industry)
end
I have also commented out
# config.use_transactional_fixtures = true
at the bottom of spec/spec_helper.rb but that didn’t help
If the company belongs to an industry, then when you create a company, it’s creating the industry for each.
You’re accounting for this by setting the industry to the companies, but you’re not saving them. Alternatively, you can: