2011-10-21 8 views
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のテストは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 

私が持っていますコメントしました

の下部にある

が、同社は業界に属している場合、それはあなたが会社を作成するときに、それはそれぞれの業界を作成しています、

+0

関連する会社を確認する前に、業界でリロードを呼び出してみましたか? @ industry.reload –

+2

業界を追加した後に会社オブジェクトを保存する必要があると思います –

答えて

2

を助けにはなりませんでした。

あなたは業界を企業に設定することでこれを考慮していますが、あなたはそれらを保存していません。または、次の操作を行うことができます。

before do 
    @industry = Factory(:industry) 
    @company1 = Factory(:company, :industry => @industry) 
    @company2 = Factory(:company, :industry => @industry) 
end 

it "should have both companies" do 
    @industry.companies.should == [@company1, @company2] 
end 
+1

素晴らしい。 btw私は '@industry.companies.count.should == 2'を使用することもできます 最後に –

関連する問題