Created
January 14, 2018 12:44
-
-
Save stevo/c1aa09e51f9316f4da61bf1c7260676d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment