Skip to content

Commit

Permalink
Add specs for persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Jan 23, 2025
1 parent 77bb74a commit f3bc221
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions spec/models/concerns/test_track/persistence_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'rails_helper'

RSpec.describe TestTrack::Persistence do
let(:resource) do
Class.new do
include TestTrack::Resource
include TestTrack::Persistence

attribute :name
attribute :saved, default: false

validates :name, presence: true

private

def persist!
self.saved = true
end
end
end

before do
stub_const('FakeResource', resource)
end

describe '#save' do
subject { resource.new(name: 'foo') }

it 'saves' do
expect(subject).to be_valid
expect(subject.save).to be(true)
expect(subject.saved).to be(true)
end

it 'does not save an invalid resource' do
subject.name = ''
expect(subject).not_to be_valid
expect(subject.save).to be(false)
expect(subject.saved).to be(false)
end

it 'handles server errors' do
allow(subject).to receive(:persist!).and_raise(Faraday::UnprocessableEntityError, 'kaboom')

expect(subject).to be_valid
expect(subject.save).to be(false)
expect(subject.saved).to be(false)
expect(subject.errors[:base]).to include('could not be saved')
end
end

describe '#save!' do
let(:subject) { resource.new(name: 'foo') }

it 'returns true when the resource is successfully saved' do
expect(subject.save!).to be(true)
end

it 'raises ActiveModel::ValidationError the resource is not saved' do
subject.name = ''
expect { subject.save! }.to raise_error(ActiveModel::ValidationError)
end

it 'converts server errors to validation errors' do
allow(subject).to receive(:persist!).and_raise(Faraday::UnprocessableEntityError, 'kaboom')

expect { subject.save! }.to raise_error(ActiveModel::ValidationError)
expect(subject.errors[:base]).to include('could not be saved')
end
end

describe '.create!' do
it 'returns a new instance when the resource is successfully saved' do
expect(resource.create!(name: 'Foo')).to be_a(resource)
end

it 'raises ActiveModel::ValidationError the resource is not saved' do
expect { resource.create!(name: '') }.to raise_error(ActiveModel::ValidationError)
end
end
end

0 comments on commit f3bc221

Please sign in to comment.