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'
1 Answers
Answers 1
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 }
0 comments:
Post a Comment