csv - How to mock file to test file upload in rails 4.2.1 -
i'm trying test action import products csv.
i'm being able first test pass. params[:file]
goes controller string "#<stringio:0x007fc0d40a0bd0>"
doesn't make test fail isn't correct behaviour.
the second test i'm getting following error
private method `gets' called #<actiondispatch::http::uploadedfile:0x007fd391de0a00>
here's spec (content csv content)
# spec/controllers/products_controller_spec.rb describe 'post import file' before post :import, file: file end context 'with invalid data' subject { spree::product.count } let(:file) { stringio.new("") } { is_expected.to eq 0 } end context 'with valid data' subject { spree::product.count } let(:file) { actiondispatch::http::uploadedfile.new(params) } let(:params) { original_filename: 'file.csv', content_type: 'text/csv', tempfile: stringio.new(content) } end { is_expected.to eq 1 } end end
try use rack::test::uploadedfile
upload test file, this
context 'with valid data' subject { spree::product.count } let(:path_to_file) { rails.root.join 'spec/fixtures/file.csv' } let(:file) { rack::test::uploadedfile.new path_to_file, 'text/csv' } { is_expected.to eq 1 } end
Comments
Post a Comment