Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec: How to verify if a record has been deleted?

I have created a simple Rspec test to verfiy if a model created has been deleted. However, the test fails because the model still exists. Can anyone provide any assistance on how to determine if the record was actually deleted?

RSpec.describe Person, type: :model do

let(:person) {
    Person.create(
      name: "Adam",
      serial_number: "1"
    )
  }
  
  it "destroys associated relationships when person destroyed" do
  person.destroy
  expect(person).to be_empty()
  end
end
like image 583
tlockhart Avatar asked Feb 08 '21 19:02

tlockhart


People also ask

How to retrieve deleted records in CRM?

There is no way to retrieve the deleted records in CRM. If the auditing is enabled you can retrieve the audit history and create the records back. Unfortunately options to restore specific records are not available yet, you would need to restore a backup point to get the records again .

How do I find out if my record has been expunged?

The quickest and easiest way to find out if your record has been expunged is to visit the court where your case was held and ask to see the records. You know that your record is clean if the court does not have your records or if it approves your request. The question is, who else has the documents?

Can a shared user delete recordings or videos?

A shared user is not able to delete any recordings or videos. If you are sharing account access at an owner level, there is no way to see the activity of these other devices. You can, however, control mobile device access through the Control Center in the Ring app.

How to retrieve deleted records from a backup?

As Giselle mentioned there isn't a way to retrieve the deleted records and you would have to restore a backup to do that. The reply is solely my personal opinion.


3 Answers

You have two choices. You can test that

  1. a record was removed from the database

    it "removes a record from the database" do
      expect { person.destroy }.to change { Person.count }.by(-1)
    end
    

    but that doesn't tell you which record was removed.

  2. Or that the exact record does not exist in the database anymore

    it "removes the record from the database" do
      person.destroy
      expect { person.reload }.to raise_error(ActiveRecord::RecordNotFound)
    end
    

    or

    it "removes the record from the database" do
      person.destroy
      expect(Person.exists?(person.id)).to be false
    end
    

    But that does not make sure that the record existed before.

A combination of both could be:

    it "removes a record from the database" do
      expect { person.destroy }.to change { Person.count }.by(-1)
      expect { person.reload }.to raise_error(ActiveRecord::RecordNotFound)
    end
like image 116
spickermann Avatar answered Nov 06 '22 12:11

spickermann


When you delete a record from a database, an object still persists in memory. That's why expect(person).to be_empty() fails.

RSpec has the change matcher. ActiveRecord has the persisted? method. It returns false if a record is not persisted in a database.

it "destroys associated relationships when rtu destroyed" do
  expect { person.destroy }.to change(Person, :count).by(-1)
  expect(person.persisted?).to be_falsey
end

destroy is a method of a framework. As far as I know, you don't need to test its methods.

like image 22
Yakov Avatar answered Nov 06 '22 12:11

Yakov


I think the following is a nice way to test a specific record has been removed with one expectation, while ensuring your testing the result of the action and not just the state of your test subject.

it "removes the record from the database" do
  expect { person.destroy }.to change { Person.exists?(person.id) }.to(false)
end
like image 30
EdemaRuh Avatar answered Nov 06 '22 12:11

EdemaRuh