In my rspec for uploaded files I am stubbing a function like this:
allow(ABCCLass::XYZProcessor).to receive(:prepare_processing).with(csv_statements).and_return(nil)
and
let(:csv_statements) do
[fixture_file_upload('transaction.csv', 'application/csv')]
end
I get error in expect: expected Rack::Test::UploadedFile got ActionDispatch::Http::UploadedFile
This makes total sense as fixture_file_upload returns Rack::Test::UploadedFile and stub is generated with it but in controller uploaded file is instance of Action::Dispatch::Uploaded file.
Any idea how can I create stub and fix this issue?
Backtrace:
-[[#<Rack::Test::UploadedFile:0x00559d3f298100 @content_type="application/csv", @original_filename="blank.csv", @tempfile=#<Tempfile:/tmp/blank.csv20180114-293-jcb2ii>, @_filename="eur-transactions.csv">,
- #<Rack::Test::UploadedFile:0x00559d3f296e40 @content_type="application/csv", @original_filename="blank.csv", @tempfile=#<Tempfile:/tmp/blank.csv20180114-293-1xbpd0t>, @_filename="gbp-statement.csv">,
- #<Rack::Test::UploadedFile:0x00559d3f295e50 @content_type="application/csv", @original_filename="blank.csv", @tempfile=#<Tempfile:/tmp/blank.csv20180114-293-1peng22>, @_filename="internal_HSBCnet1.xls">]]
+[[#<ActionDispatch::Http::UploadedFile:0x00559d3cb3a6a8 @tempfile=#<Tempfile:/tmp/RackMultipart20180114-293-1l9ma69.csv>, @original_filename="eur-transactions.csv", @content_type="application/csv", @headers="Content-Disposition: form-data; name=\"bank_statements[]\"; filename=\"eur-transactions.csv\"\r\nContent-Type: application/csv\r\nContent-Length: 0\r\n">,
+ #<ActionDispatch::Http::UploadedFile:0x00559d3cb3a608 @tempfile=#<Tempfile:/tmp/RackMultipart20180114-293-17oxrrk.csv>, @original_filename="gbp-statement.csv", @content_type="application/csv", @headers="Content-Disposition: form-data; name=\"bank_statements[]\"; filename=\"gbp-statement.csv\"\r\nContent-Type: application/csv\r\nContent-Length: 0\r\n">,
+ #<ActionDispatch::Http::UploadedFile:0x00559d3cb3a568 @tempfile=#<Tempfile:/tmp/RackMultipart20180114-293-2ukuul.xls>, @original_filename="internal_HSBCnet1.xls", @content_type="application/csv", @headers="Content-Disposition: form-data; name=\"bank_statements[]\"; filename=\"internal_HSBCnet1.xls\"\r\nContent-Type: application/csv\r\nContent-Length: 0\r\n">]]
Please stub a default value first if message might be received with other args as well.
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-support-3.7.0/lib/rspec/support.rb:97:in `block in <module:Support>'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-support-3.7.0/lib/rspec/support.rb:106:in `call'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-support-3.7.0/lib/rspec/support.rb:106:in `notify_failure'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/error_generator.rb:327:in `notify'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/error_generator.rb:311:in `__raise'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/error_generator.rb:60:in `raise_missing_default_stub_error'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/proxy.rb:206:in `raise_missing_default_stub_error'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/proxy.rb:191:in `message_received'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/proxy.rb:326:in `message_received'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/method_double.rb:77:in `proxy_method_invoked'
# /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.7.0/lib/rspec/mocks/method_double.rb:64:in `block (2 levels) in define_proxy_method'
# ./app/controllers/admin/daily_adjustments/bank_transfer_deposits_controller.rb:25:in `start_process'
Instead of calling fixture_file_upload('transaction.csv', 'application/csv')
in the test, you should put that return value instead.
I would use an instance double if you want to stub it. Something like:
let(:csv_statements) { instance_double(Rack::Test::UploadedFile, content_type: 'application/csv', original_filename: 'blank.csv', file_name: 'eur-transactions.csv') }
This might not work via copy and paste but this will stub an INSTANCE of that class and whenever those messages are received, it will return those values. I don't know what happens inside of "fixture_file_upload", but whatever Rack::Test::UploadedFile object is returned, just stub it like so. If you want to do a class stub, it's very similar:
let(:csv_statements) { class_double(Rack::Test::UploadedFile, content_type: 'application/csv', original_filename: 'blank.csv', file_name: 'eur-transactions.csv').as_stubbed_const }
解决方案是使用ActionDispatch::Http::UploadedFile
生成存根,因为目的是匹配实际函数的签名和参数。
An old issue, however I ran into this recently where I was testing that the correct params were being passed to another class, including the uploaded csv file.
Workaround was to stub ActionDispatch::Http::UploadedFile
and have its :new
method return the instance of Rack::Test::UploadedFile:
that fixture_file_upload
creates.
let(:csv_file) do
path = Rails.root.join('spec', 'fixtures', 'test.csv')
fixture_file_upload(path, 'text/csv')
end
before do
allow(ActionDispatch::Http::UploadedFile).to receive(:new)
.and_return(csv_file)
end
it 'passes csv file to class' do
perform
expect(MyClass).to have_received(:new).once.with(file: csv_file)
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.