Skip to content

Testing

Granite has multiple helpers for your rspec tests. Add require 'granite/rspec' to your rails_helper.rb in order to use them. All specs that live in spec/apq/actions/ will get tagged with :granite_action type and will have access to granite action specific helpers. All specs that live in spec/apq/projectors/ will get tagged with :granite_projector type and will have access to granite projector specific helpers.

Subject

subject(:action) { described_class.as(performer).new(user, attributes) }
let(:user) { User.new }
let(:attributes) { {} }

Projectors

it { is_expected.to have_projector(:simple) }

Test overridden projector methods:

describe 'projectors', type: :granite_projector do
  subject { action.modal }
  projector { described_class.modal }

  its(:perform_success_response) { is_expected.to eq(my_success: 'yes') }
end

Policies

subject { described_class.as(User.new).new }
it { is_expected.to be_allowed }

Preconditions

context 'correct initial state' do
  it { is_expected.to satisfy_preconditions }
end

context 'incorrect initial state' do
  let(:company) { build_stubbed(:company, :active) }
  it { is_expected.not_to satisfy_preconditions.with_message("Some validation message")}
  it { is_expected.not_to satisfy_preconditions.with_messages(["First validation message", "Second validation message"])}
end

Validations

Validations tests are no different to ActiveRecord models tests

Perform

Run the action using perform! to test side-effects:

specify { expect { perform! }.to change(User, :count).by(1) }