From 37b7340eb134bc723783fc2fc56f2a00664f6c4d Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 13 Jan 2021 17:41:16 +0100 Subject: [PATCH] Refactor specs to speed them up We don't care about the conversion from hash to JSON (that's an ActiveModel::Serializer responsibility that is thoroughly tested) but our logic so we can skip that step which only slows down tests. It consistently reduced ~1.5s on my machine but it's still too slow to wait ~8.5s to get feedback from them. --- spec/serializers/api/order_serializer_spec.rb | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/spec/serializers/api/order_serializer_spec.rb b/spec/serializers/api/order_serializer_spec.rb index bc1115fc90..d635cf0ada 100644 --- a/spec/serializers/api/order_serializer_spec.rb +++ b/spec/serializers/api/order_serializer_spec.rb @@ -6,21 +6,28 @@ describe Api::OrderSerializer do let(:serializer) { Api::OrderSerializer.new order } let(:order) { create(:completed_order_with_totals) } - let!(:completed_payment) { create(:payment, order: order, state: 'completed', amount: order.total - 1) } - let!(:payment) { create(:payment, order: order, state: 'checkout', amount: 123.45) } + describe '#serializable_hash' do + let!(:completed_payment) do + create(:payment, order: order, state: 'completed', amount: order.total - 1) + end + let!(:payment) { create(:payment, order: order, state: 'checkout', amount: 123.45) } - it "serializes an order" do - expect(serializer.to_json).to match order.number.to_s - end + it "serializes an order" do + expect(serializer.serializable_hash[:number]).to eq(order.number) + end - it "convert the state attributes to translatable keys" do - expect(serializer.to_json).to match "complete" - expect(serializer.to_json).to match "balance_due" - end + it "convert the state attributes to translatable keys" do + hash = serializer.serializable_hash - it "only serializes completed payments" do - expect(serializer.to_json).to match completed_payment.amount.to_s - expect(serializer.to_json).to_not match payment.amount.to_s + expect(hash[:state]).to eq("complete") + expect(hash[:payment_state]).to eq("balance_due") + end + + it "only serializes completed payments" do + hash = serializer.serializable_hash + + expect(hash[:payments].first[:amount]).to eq(completed_payment.amount) + end end describe '#outstanding_balance' do