# Poor specs
# ==========
# 3rd party library and framework methods are used directly to facilitate tests
describe ExportActiveOrders do
describe '.call' do
it 'exports all active orders and returns a path to exported file' do
create_list(:order, 2, :active)
create(:order, :inactive)
path = ExportActiveOrders.call
expected_columns = Order.where(active: true).pluck(:customer_name, :total_value)
book = Spreadsheet.open(path)
sheet = book.worksheet(0)
expect(sheet.rows).to match_array(expected_columns)
end
end
end
# Good specs
# ============
# 3rd party library usage is abstracted away to helper. Instead of using framework methods
# to retrieve expected values, those values are provided explicitly.
module Support
module XlsHelpers
def open_spreadsheet(path)
book = Spreadsheet.open(path)
sheet = book.worksheet(0)
sheet.rows
end
end
end
RSpec.configure do |config|
config.include Support::XlsHelpers
end
describe ExportActiveOrders do
describe '.call' do
it 'exports all active orders and returns a path to exported file' do
create(:order, :active, customer_name: 'Bruce Wayne', total_value: 1000)
create(:order, :inactive, customer_name: 'Dr. Strange', total_value: 1200)
create(:order, :active, customer_name: 'Edward Nygma', total_value: 500)
path = ExportActiveOrders.call
rows = open_spreadsheet(path)
expect(rows).to match_array([['Bruce Wayne', 1000],['Edward Nygma', 500]])
end
end
end