Skip to content
Techbrunch edited this page Dec 5, 2017 · 10 revisions

Using the rspec matcher

There is an rspec matcher available with the gem.

require 'rails_helper'

RSpec.describe Foo, type: :model do
  it { is_expected.to act_as_paranoid }
end

Using shared examples

You can use shared examples to help test that models are including acts_as_paranoid properly. For example, the following shared examples can be created in spec/support/shared_examples/paranoia_examples.rb:

shared_examples_for 'a Paranoid model' do

  it { is_expected.to have_db_column(:deleted_at) }

  it { is_expected.to have_db_index(:deleted_at) }

  it 'adds a deleted_at where clause' do
    expect(described_class.all.where_sql).to include('`deleted_at` IS NULL')
  end

  it 'skips adding the deleted_at where clause when unscoped' do
    expect(described_class.unscoped.where_sql.to_s).not_to include('`deleted_at`')  # to_s to handle nil.
  end
end

And then all you need to do is include this in the specs for the model that should be acting as paranoid, like so:

it_behaves_like 'a Paranoid model'

The above example uses Shoulda Matchers gem to work correctly (https://github.com/thoughtbot/shoulda-matchers).