我正在努力在我的单元测试中涵盖我模型的所有方法、关联和验证,到目前为止一切都很好。我订阅了 TreeHouse 并观看了Ruby Foundations -虔诚地测试。
我昨晚在为我的 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 方法在我不同的环境中运行良好。我只是不明白为什么它没有返回测试中的唯一位置列表。
任何想法?
谢谢,弗朗西斯