From 5845ace8e9a9c994abffdac0a8b4b9a080b84635 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 19 Apr 2021 12:23:30 +0200 Subject: [PATCH] Don't persist models to test serializer Serializers don't care whether or not the objects they go through are persisted in DB. They just convert them. --- .../api/current_order_serializer_spec.rb | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/spec/serializers/api/current_order_serializer_spec.rb b/spec/serializers/api/current_order_serializer_spec.rb index dd5be4c180..6317477a0f 100644 --- a/spec/serializers/api/current_order_serializer_spec.rb +++ b/spec/serializers/api/current_order_serializer_spec.rb @@ -3,11 +3,17 @@ require 'spec_helper' describe Api::CurrentOrderSerializer do - let(:distributor) { create(:distributor_enterprise) } - let(:order_cycle) { create(:simple_order_cycle) } - let(:line_item) { create(:line_item, variant: create(:variant)) } - let(:order) { create(:order, line_items: [line_item]) } - let(:serializer) { Api::CurrentOrderSerializer.new(order, current_distributor: distributor, current_order_cycle: order_cycle).to_json } + let(:distributor) { build(:distributor_enterprise) } + let(:order_cycle) { build(:simple_order_cycle) } + let(:line_item) { build(:line_item, variant: create(:variant)) } + let(:order) { build(:order, line_items: [line_item]) } + let(:serializer) do + Api::CurrentOrderSerializer.new( + order, + current_distributor: distributor, + current_order_cycle: order_cycle + ).to_json + end it "serializers the current order" do expect(serializer).to match order.id.to_s @@ -28,7 +34,11 @@ describe Api::CurrentOrderSerializer do end context 'when there is a shipment' do - before { create(:shipment, order: order) } + let(:shipping_method) { build(:shipping_method) } + + before do + allow(order).to receive(:shipping_method).and_return(shipping_method) + end it 'includes the shipping method of the order' do expect(serializer).to match("\"shipping_method_id\":#{order.shipping_method.id}")