Add a spec for CreateStats service

This commit is contained in:
Eugene Burmakin 2024-04-02 23:26:26 +02:00
parent 48962e87e8
commit 111667ce5a
2 changed files with 40 additions and 0 deletions

View file

@ -12,5 +12,11 @@ RSpec.describe ImportJob, type: :job do
it 'creates points' do
expect { perform }.to change { Point.count }.by(8)
end
it 'calls StatCreatingJob' do
expect(StatCreatingJob).to receive(:perform_later).with(user.id)
perform
end
end
end

View file

@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe CreateStats do
describe '#call' do
subject(:create_stats) { described_class.new(user_ids).call }
let(:user_ids) { [user.id] }
let(:user) { create(:user) }
context 'when there are no points' do
it 'does not create stats' do
expect { create_stats }.not_to change { Stat.count }
end
end
context 'when there are points' do
let!(:import) { create(:import, user: user) }
let!(:point_1) { create(:point, import: import, latitude: 0, longitude: 0) }
let!(:point_2) { create(:point, import: import, latitude: 1, longitude: 2) }
let!(:point_3) { create(:point, import: import, latitude: 3, longitude: 4) }
it 'creates stats' do
expect { create_stats }.to change { Stat.count }.by(1)
end
it 'calculates distance' do
create_stats
expect(Stat.last.distance).to eq(563)
end
end
end
end