tl;dr: I was trying to test that the correct items were added to an array. When I ran the test, it said that things were being added to the array indiscriminately. However, when I put a binding.pry in the first line of LessonPackage.available, the tests pass... but now, two other tests fail.
Now here's some code.
In the original test, I test that the correct number of items are added to an array. This tests the :current scope. Here's the test:
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
Notice that @old_package should have a 'current' attribute of false. Here's the code under test:
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
The test fails! But I know this code works, not only because it works in development mode, but because when I put binding.pry in the first line of LessonPackage.available, it suddenly works! But now another test is broken:
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
The reason given is that the lesson package attached to the first lesson booking has a different id than the original lesson package created. Note: THIS TEST WAS WORKING BEFORE. The ONLY thing different is the binding.pry.
I'm using database_cleaner, and this is how I have it configured:
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