Rails - testing scoped destroy_all ignores scope

137 Views Asked by At

I've set up a soft-delete functionality (not using the Paranoia gem): a user can be soft-deleted and then a scoped destroy_all permanently deletes all soft-deleted users. It works in development but I can't get the testing to pass. The test queries with the scope but destroys all users regardless.

The scope in User.rb scope :only_deleted, -> { where.not(deleted_at: nil) } finds all soft-deleted users. In the controller, I have:

def destroy_soft_deleted
  User.only_deleted.destroy_all
  ...
end

I tried this test (and many other combinations):

def setup
  @user1 = users(:adam)
  @user2 = users(:beth)
end
test "successful permanent deletion of all soft-deleted users" do
  @user2.soft_delete
  assert_difference "User.count", -1 do
    delete destroy_soft_deleted_users_path
  end
end

All other tests pass (e.g. @user.soft_delete is tested) and fixtures are good. The test log shows the scope is being used but it deletes both users anyway:

SELECT "users".* FROM "users" WHERE ("users"."deleted_at" IS NOT NULL)
DELETE FROM "users" WHERE "users"."id" = ?[0m  [["id", 585460615]]
DELETE FROM "users" WHERE "users"."id" = ?[0m  [["id", 911064266]]

Here's the test result:

"User.count" didn't change by -1.
  Expected: 1
  Actual: 0

Does Model.some_scope.destroy_all not work in tests? Is there a different assertion I should use?

0

There are 0 best solutions below