I have not tested the connection between the two models.
A course has many enrollments, an enrollment has one course.
When a course is destroyed, all records connected to it are set to active = false. This works with real objects, I just can't get the test to work, because no matter what I do, the course is not destroyed.
describe Enrollment do
it "deactivates enrollment" do
course = create(:a_course)
user = create_user
enrollment = build(:enrollment)
enrollment.course = course
enrollment.user = user
enrollment.save
expect { course.destroy }.to change { enrollment.active }.to be_false
end
end
I couldnโt find anything about destroying the factory_girl object in the factory_girl docs, maybe I am doing it all wrong and should I use โrealโ objects? Thank!
Update
Here is the model where the change occurs.
class Course < ActiveRecord::Base
attr_accessible ...
has_many :users, through: :enrollments
has_many :enrollments
before_destroy :deactivate_enrollments
protected
def deactivate_enrollments
enrollments = self.enrollments
enrollments.each do |e|
e.active = false
e.save
end
end
end
As I'm not sure about this, the course I use for testing is a factory_girl object. He was not created as follows: Course.create.... Does the factory_girl object have the same methods as the ActiveRecord object?
factory_girl:
FactoryGirl.define do
factory :course, class: Course do
titel "Course title"
end
end
2
failure message
Enrollment
deactivates enrolment (FAILED - 1)
Failures:
1) Enrollment deactivates enrollment
Failure/Error: expect { course.destroy }.to change(enrollment, :active).from(true).to(false)
active should have been changed to false, but is now true
# ./spec/models/enrollment_spec.rb:18:in `block (2 levels) in <top (required)>'
3
, . Course.destroy_all, course.destroy . , factory_girl . ?