tl; dr:我试图测试将正确的项目添加到数组中。当我运行测试时,它说事情正在被不加选择地添加到阵列中。但是,当我将一个binding.pry放在LessonPackage.available的第一行时,测试通过......但是现在,另外两个测试失败了。
现在这里是一些代码。
在原始测试中,我测试将正确数量的项添加到数组中。这测试:当前范围。这是测试:
describe 'manage multiple lessons' do
before :all do
@six_package = create(:lesson_package)
@thirteen_package = create(:lesson_package, name: 'thirteen', number_of_lessons: 13)
@old_package = create(:lesson_package, name: 'canceled', current: false)
@instructor = create(:instructor, base_lesson_price: 50, contract_rate: 40)
end
context 'available' do
it 'return array of all available package managers when given defaults' do
packages = LessonPackage.available(@instructor)
packages.length.should be 2
packages[0].class.should be LessonPackageManager
end
end
end
请注意,@ old_package的'current'属性应为false。这是测试中的代码:
class LessonPackage < ActiveRecord::Base
...
scope :current, where(current: true)
...
def self.available(instructor, method = 'self', lesson_type = 'private')
LessonPackage.current.map do |package|
manager = LessonPackageManager.make_package(instructor, package, lesson_type)
manager.send(method)
end
end
end
测试失败!但我知道这段代码有效,不仅因为它在开发模式下工作,而且因为当我将binding.pry放在LessonPackage.available的第一行时,它突然起作用了!但现在另一项测试被打破了:
describe 'managed lesson' do
before :all do
@six_package = create(:lesson_package)
@instructor = create(:instructor, base_lesson_price: 50, contract_rate: 35)
end
describe 'booking lessons with a student' do
before :each do
@student = create(:student, area: 'herethere')
package_manager = LessonPackage.manage('six', @instructor)
package_manager.add_to_cart(@student)
end
subject {LessonBooking.first}
its(:lesson_package) {should eq @six_package}
end
end
end
给出的原因是第一课预订附带的课程包的ID与创建的原始课程包不同。注意:此测试之前正在进行。唯一不同的是binding.pry。
我正在使用database_cleaner,这就是我配置它的方式:
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end