Created
January 14, 2018 11:15
-
-
Save stevo/43a4ac7e3ef22972d0e6bf706bb0d060 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 | |
# ========== | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment