stevo
1/14/2018 - 12:44 PM

most_profitable_orders_query_spec.rb

# Poor specs
# ==========

# Customer relation is irrelevant in this case. Same as each order's subject.
# `match_array` expectation verifies count implicitly, so additional check seems obsolete.
describe MostProfitableOrdersQuery do
  describe '.call' do
    it 'returns top 50% orders with the biggest total value' do
      customer = create(:customer, name: 'Tony stark')
      create(:order, customer: customer, subject: 'Iron armor', total_value: 1000)
      most_profitable_order_1 =
        create(:order, customer: customer, subject: 'Dual laser glove', total_value: 1200)
      most_profitable_order_2 =
        create(:order, customer: customer, subject: 'Power cells', total_value: 1500)
      create(:order, customer: customer, subject: 'Sensor array', total_value: 500)

      result = MostProfitableOrdersQuery.call

      expect(result).to be_kind_of(ActiveRecord::Relation)
      expect(result.count).to eq(2)
      expect(result).to match_array(most_profitable_order_1, most_profitable_order_2)
    end
  end
end

# Good specs
# ============

# Only data important from test's perspective is provided.
describe MostProfitableOrdersQuery do
  describe '.call' do
    it 'returns top 50% orders with the biggest total value' do
      create(:order, total_value: 1000)
      most_profitable_order_1 = create(:order, total_value: 1200)
      create(:order, total_value: 500)
      most_profitable_order_2 = create(:order, total_value: 1500)

      result = MostProfitableOrdersQuery.call

      expect(result).to be_kind_of(ActiveRecord::Relation)
      expect(result).to match_array(most_profitable_order_1, most_profitable_order_2)
    end
  end
end