对于我的 Rails 4 应用程序,我想测试删除 Course 对象是否会删除关联的 Chapter 对象。
在那里,在我的课程模型中,我将关联定义如下:
has_many :chapters, dependent: :delete_all
这工作正常。但是,我不知道如何为此级联删除编写测试。
我的 Rspec 测试如下所示:
before do
@course = Fabricate(:course)
end
it "deletes associated chapters" do
set_current_admin
chapter1 = Fabricate(:chapter, course_id: @course_id)
delete :destroy, id: @course.id
expect(Chapter.count).to eq(0)
end
我希望 Chapter.all 为 0 但是当我运行这个测试时,我得到了这个结果:
1) Admin::CoursesController DELETE #destroy deletes associated chapters
Failure/Error: expect(Chapter.all).to eq(0)
expected: 0
got: #<ActiveRecord::Relation [#<Chapter id: 709, title: "omnis corrupti eos et aperiam", description: "Alias sunt tempore aut deserunt. Optio ea assumenda...", course_id: nil, created_at: "2013-11-01 07:10:28", updated_at: "2013-11-01 07:10:28", tagline: "Omnis nemo praesentium corporis. Doloremque dolorib...", badge_image: nil>]>
(compared using ==)
# ./spec/controllers/admin/courses_controller_spec.rb:181:in `block (3 levels) in <top (required)>'
我怎样才能使这个测试通过?
谢谢你的帮助,
安东尼