Hi, I’m testing the dependent: :destroy functionality for a many-to-many association in a TDD way. So to check that the associated records are deleted in the join table (theme_projects) I wrote this integration test:
scenario "Dependent associations are destroyed when admin deletes research project" do research_theme_1 = Fabricate(:research_theme) research_theme_2 = Fabricate(:research_theme) @research_project.research_themes << research_theme_1 @research_project.research_themes << research_theme_2 themes = @research_project.research_themes click_link "Delete" themes.each do |theme| theme.research_projects.should_not include(@research_project) end end
I expected this test to fail, becauce I hadn’t yet added dependent: :destroy to my many to many association:
class ResearchProject < ActiveRecord::Base validates :title, :body, presence: true has_many :theme_projects has_many :research_themes, through: :theme_projects end
To my surprise however the test passes. When I do a sanity check, the records for the deleted research project are not removed from the join tabel.
So is my test not valid?
When I add the dependent: :destroy directive to my many to many association, the records for a deleted research project are removed from the join table.
My repo is at: https://github.com/acandael/posplus-rails
the integration test is at: spec/features/admin_handles_research_projects_spec.rb
Thanks for your help,