I have in project.rb:
has_many :items, :dependent => :destroy
And in item.rb:
belongs_to :project
My projects fixture:
b1_s_first_project:
title: B1's first project
And my items fixture:
b1_s_first_project_s_first_item:
title: B1's first project's first item
project: b1_s_first_project
In my unit test, I set local variables item = items(:b1_s_first_project_s_first_item)
and project = projects(:b1_s_first_project)
. When I call project.destroy
, project.destroyed?
returns true, but item.destroyed?
returns nil, as if it hadn't been destroyed. What am I missing? Thanks in advance.
看来您可能需要先添加item.reload
然后再测试它是否被破坏
Just to provide an alternative method by leveraging .reload
on the association record using the assertion assert_raise
. This works under Rails 5, but should work in the the previous two versions.
Model set-up:
# foo.rb
class Foo < ApplicationRecord
has_many :bars, dependent: :destroy
end
# bar.rb
class Bar < ApplicationRecord
belongs_to :foo
end
Fixture set-up:
# foos.yml
oof:
title: Hello
# bars.yml
rab:
title: World!
foo: oof
Model minitest:
test 'foo should destroy dependency bar on destroy' do
foo, bar = foos(:off), bars(:rab)
assert foo.destroy
assert_raise(ActiveRecord::RecordNotFound) { bar.reload }
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.