2013-04-11 89 views
0

我正在努力涵蓋我的單元測試中所有模型的方法,關聯和驗證,並且到目前爲止它效果很好。我已經訂閱了TreeHouse並且虔誠地觀看了Ruby Foundations - Testing需要幫助瞭解爲什麼單元測試方法不起作用

我昨晚無意中發現了這個錯誤,測試方法爲我Provider.rb型號:

class Provider < ActiveRecord::Base 

    attr_accessible :description, :name 
    validates :name, :presence => true 
    validates :description, :presence => true 
    validates :name, :length => { :minimum => 6, :maximum => 100 } 
    validates :description, :length => { :minimum => 6, :maximum => 100 } 
    has_many :courses 
    resourcify 

    def unique_locations 
    Location.joins(sessions: :course).where(courses: { provider_id: self.id }).uniq.pluck('locations.name') 
    end 

end 

我對unique_locations方法單元測試如下:

test 'should return a list of unique locations' do 
    provider = FactoryGirl.build(:provider) 
    assert_equal provider.unique_locations, ["location_1", "location_2"] 
    end 

我得到的運行我的測試後出現以下錯誤:

<[]> expected but was 
<["location_1", "location_2"]>. 

我的工廠很簡單:

FactoryGirl.define do 
    factory :course do 
    name 'Snowboard 101' 
    description 'Snowboard course' 
    association :provider, factory: :provider 
    end 
end 

FactoryGirl.define do 
    factory :provider do 
    name 'The School of Hard Knocks' 
    description 'School description' 
    end 
end 

FactoryGirl.define do 
    factory :session_snowboard, class: Session do 
    name 'Winter Session' 
    description 'Snowboarding 101' 
    price 200 
    class_size 4 
    association :course, factory: :course 
    association :location, factory: :location_1 
    end 

    factory :session_ski, class: Session do 
    name 'skiing 101' 
    description 'Start in november' 
    price 100 
    class_size 4 
    association :course, factory: :course 
    association :location, factory: :location_2 
    end 

end 

FactoryGirl.define do 
    factory :location_1 do 
    name 'location_1' 
    end 

    factory :location_2 do 
    name 'location_2' 
    end 

end 

我的unique_locations方法在我的不同環境中工作正常。我只是無法弄清楚爲什麼它沒有返回測試中唯一位置的列表。

任何想法?

感謝, 弗朗西斯

回答

0

所以經過多次打探與Factory_Girl我找到了解決辦法:

這裏是我的工廠:

FactoryGirl.define do 
    factory :provider do 
    name 'McGill University' 
    description 'McGill is one of the best universities in the world.' 
    after(:create) {|provider| create_list(:course, 2, provider: provider) } 
    end 
end 

FactoryGirl.define do 
    factory :course do 
    name 'Snowboard 101' 
    description 'Snowboard course' 
    provider 
    after(:create) {|course| create_list(:session, 2, course: course) } 
    end 
end 

FactoryGirl.define do 
    factory :session do 
    name 'Winter Session' 
    description 'Snowboarding 101' 
    price 150 
    class_size 4 
    course 
    location 
    end 
end 

FactoryGirl.define do 
    factory :location do |l| 
    l.sequence(:name) { |n| "location_#{n}"} 
    end 
end 

我也更新了我的測試:

test 'should return a list of unique locations' do 
    provider = create(:provider) 
    assert_equal provider.unique_locations, %w[location_1 location_2 location_3 location_4] 
    end 

此創建成功4個不同的地點。

0

要調用FactoryGirl.build(:provider)只有建立並不會保存到數據庫,所以unique_locations顯然不會有ID去了。使用create它應該沒問題。

+0

感謝您的回覆,我正在使用.create,但仍然收到相同的錯誤消息。 – 2013-04-11 17:24:10