1
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個測試都可以。 問題是,當我嘗試使用相關的對象。rspec - 如何獲得測試通過
我的車型有:
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
只需插入記錄本身似乎就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
順便說一句我的規格的頂部是:
require 'spec_helper'
describe Industry do
before(:each) do
@industry = Factory(:industry)
end
我有也已註釋掉
# config.use_transactional_fixtures = true
在spec/spec_helper.rb
底部,但沒有幫助
您是否在嘗試調用與工業相關的公司之前調用reload? @ industry.reload –
我認爲你需要在添加行業 –