tl; dr:我试图测试是否将正确的项目添加到数组中。当我运行测试时,它说东西被不加选择地添加到数组中。但是,当我将 binding.pry 放在 LessonPackage.available 的第一行时,测试通过了……但是现在,另外两个测试失败了。
现在这里有一些代码。
在原始测试中,我测试是否将正确数量的项目添加到数组中。这将测试 :current 范围。这是测试:
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 的“当前”属性应为 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