Mocking file uploads in Rails 3.1 controller tests

放肆的年华 提交于 2019-12-03 12:40:14

Finally found this, which tells that although the thing returned by fixture_file_upload has a @tempfile member, it lacks the reader method. Solved as follows

  FileUtils.touch('file.zip') # fixture_file_upload needs the file to exist
  @file = fixture_file_upload('file.zip')
  class << @file
    # The reader method is present in a real invocation,
    # but missing from the fixture object for some reason (Rails 3.1.1)
    attr_reader :tempfile
  end

I went around this way

upload_file = fixture_file_upload('files/stats_upload.csv', 'text/csv')
upload_file.stubs(:tempfile).returns(upload_file)

I made a pull request to fix this issue, please +1 if you like it: https://github.com/brynary/rack-test/pull/67

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!