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のテストはOKです。 問題は、関連するオブジェクトを使用しようとするときです。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
ちょうどレコードを挿入自体が正常に動作するように見えるん -
が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
私が持っていますコメントしました
の下部にあるが、同社は業界に属している場合、それはあなたが会社を作成するときに、それはそれぞれの業界を作成しています、
関連する会社を確認する前に、業界でリロードを呼び出してみましたか? @ industry.reload –
業界を追加した後に会社オブジェクトを保存する必要があると思います –