Skip to content

Commit 24e9e78

Browse files
committed
Test the yielding not the end output
1 parent ae77992 commit 24e9e78

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

app/controllers/distributions_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def index
7575
respond_to do |format|
7676
format.html
7777
format.csv do
78-
send_stream filename: "Distributions-#{Time.zone.today}.csv" do |stream|
79-
Exports::ExportDistributionsCSVService.new(distributions: @distributions.includes(line_items: :item), organization: current_organization, filters: scope_filters).generate_csv_stream do |row|
78+
send_stream filename: "Distributions-#{Time.zone.today}.csv" do |stream|
79+
Exports::ExportDistributionsCSVService.new(distributions: @distributions.includes(line_items: :item), organization: current_organization, filters: scope_filters).generate_csv_stream do |row|
8080
stream.write(row)
8181
end
8282
end

spec/services/exports/export_distributions_csv_service_spec.rb

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
describe '#generate_csv_stream' do
99
subject do
10-
rows = ""
11-
described_class.new(distributions: distributions, organization: organization, filters: filters).generate_csv_stream do |row|
12-
rows << row
13-
end
14-
rows
10+
Proc.new { described_class.new(distributions: distributions, organization: organization, filters: filters).generate_csv_stream }
1511
end
1612

1713
let(:duplicate_item) { create(:item, name: "Dupe Item", value_in_cents: 300, organization: organization, package_size: 2) }
@@ -66,7 +62,9 @@
6662
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0,2,0,0
6763
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0,0,0,3
6864
CSV
69-
expect(subject).to eq(csv)
65+
subject do |subject_row|
66+
expect(row).to eq(csv.next)
67+
end
7068
end
7169
end
7270

@@ -81,7 +79,9 @@
8179
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0.00,0,0.00,2,60.00,0,0.00,0,0.00
8280
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0.00,0,0.00,0,0.00,0,0.00,3,120.00
8381
CSV
84-
expect(subject).to eq(csv)
82+
subject do |subject_row|
83+
expect(row).to eq(csv.next)
84+
end
8585
end
8686
end
8787

@@ -96,7 +96,9 @@
9696
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0,0,0,2,0,0,0,0,0
9797
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0,0,0,0,0,0,0,3,0
9898
CSV
99-
expect(subject).to eq(csv)
99+
subject do |subject_row|
100+
expect(row).to eq(csv.next)
101+
end
100102
end
101103
end
102104

@@ -112,7 +114,9 @@
112114
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0.00,0,0,0.00,0,2,60.00,0,0,0.00,0,0,0.00,0
113115
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0.00,0,0,0.00,0,0,0.00,0,0,0.00,0,3,120.00,0
114116
CSV
115-
expect(subject).to eq(csv)
117+
subject do |subject_row|
118+
expect(row).to eq(csv.next)
119+
end
116120
end
117121
end
118122

@@ -134,13 +138,24 @@
134138
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0,2,0,0,0
135139
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0,0,0,3,0
136140
CSV
137-
expect(subject).to eq(csv)
141+
142+
subject do |subject_row|
143+
expect(row).to eq(csv.next)
144+
end
138145
end
139146
end
140147

141148
context 'reporting category column' do
142149
let(:filters) { {} }
143150

151+
subject do
152+
rows = ""
153+
described_class.new(distributions: distributions, organization: organization, filters: filters).generate_csv_stream do |row|
154+
rows << row
155+
end
156+
rows
157+
end
158+
144159
it 'includes a Reporting Category column with the humanized category for each distribution' do
145160
csv = CSV.parse(subject, headers: true)
146161
expect(csv.headers).to include("Reporting Category")
@@ -185,7 +200,9 @@
185200
csv = <<~CSV
186201
Partner,Initial Allocation,Scheduled for,Source Inventory,Total Number of #{item_name},Total Value of #{item_name},Delivery Method,Shipping Cost,Status,Agency Representative,Comments,Reporting Category,Dupe Item
187202
CSV
188-
expect(subject).to eq(csv)
203+
subject do |subject_row|
204+
expect(row).to eq(csv.next)
205+
end
189206
end
190207
end
191208
end

0 commit comments

Comments
 (0)