###前提・実現したいこと
商品管理アプリをrailsを使って実装しています。
サービスクラスで.xlsmの拡張子のつくファイルで商品登録を一括登録できるような実装をしていて、それのテストコード(rspec)を書いている所です。
テストを通したいです。
###発生している問題・エラーメッセージ
[1] pry(#<RSpec::ExampleGroups::XlsImportProductImport::WithValidAttributes>)> XlsImport::ProductImport.new(xlsm) ArgumentError: Can't detect the type of /var/folders/t7/kv9bmf492k5fkv7_97888vjr0000gn/T/sample.xlsm20170503-11249-1ytwhrt - please use the :extension option to declare its type. from /Users/hiroakishimohira/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/roo-2.7.1/lib/roo/spreadsheet.rb:13:in `rescue in open'
###該当のソースコード
ruby
1#controller 2def create 3 importer = Hoge::ProductImport.new(params[:xls_file].last) 4#...その後の処理 5end 6 7#service 8module Hoge 9 class ProductImport 10 def initialize(file) 11 @file = Roo::Spreadsheet.open(file) 12 end 13 end 14#...その後の保存の処理 15end 16 17#rspec 18require 'rails_helper' 19RSpec.describe Hoge::ProductImport do 20 describe 'with valid attributes' do 21 let(:xlsm) { fixture_file_upload('xlsm/sample.xlsm', 'application/vnd.ms-excel') } 22 include Hoge 23 24 it 'create employee by importing xls' do 25 importer = Hoge::ProductImport.new(xlsm) 26 expect { importer.import }.to change { hoge.count }.by(10) 27 end 28 end 29end 30 31
###試したこと
fixture_file_uploadを使うための,rails_helper.rbなどの記述はちゃんとしているつもりです。
fixture_file_upload について調べたりしましたがいまいちわかりません。。。
https://apidock.com/rails/ActionController/TestProcess/fixture_file_upload
また実際のdevelopmentでの送られるparams[:xls_file]の内容↓
#<ActionDispatch::Http::UploadedFile:0x007f89b9724508 @content_type="application/vnd.ms-excel.sheet.macroenabled.12", @headers="Content-Disposition: form-data; name=\"xls_file[]\"; filename=\"sample.xlsm\"\r\nContent-Type: application/vnd.ms-excel.sheet.macroenabled.12\r\n", @original_filename="sample.xlsm", @tempfile=#<File:/var/folders/t7/kv9bmf492k5fkv7_97888vjr0000gn/T/RackMultipart20170503-10333-8kf7pg.xlsm>>
rspecでのtestでのxlsmの内容↓
#<Rack::Test::UploadedFile:0x007fabc825a780 @content_type="application/vnd.ms-excel", @original_filename="sample.xlsm", @tempfile= @tempfile=#<File:/var/folders/t7/kv9bmf492k5fkv7_97888vjr0000gn/T/sample.xlsm20170503-11396-10tt2eg>>
それぞれの違いを比較したりとしましたがいまいち原因がわかりません。。。
今までこのようなエラーが起きたことある方、同じようなrspec書いたことある方、解決法を知っている方などいたらお助けくださいm(_ _)m
###補足
上記の説明で何か不明な点があればご遠慮なくお申し付けくださいm(_ _)m
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/03 13:42