我正在努力涵蓋我的單元測試中所有模型的方法,關聯和驗證,並且到目前爲止它效果很好。我已經訂閱了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方法在我的不同環境中工作正常。我只是無法弄清楚爲什麼它沒有返回測試中唯一位置的列表。
任何想法?
感謝, 弗朗西斯
感謝您的回覆,我正在使用.create,但仍然收到相同的錯誤消息。 – 2013-04-11 17:24:10