Mocking file uploads in Rails 3.1 controller tests

前端 未结 3 1580
一个人的身影
一个人的身影 2021-02-15 04:08

My controller accesses the tempfile attribute of an uploaded file and passes it to another mocked component. My test code has

  @file = mock(Object)         


        
相关标签:
3条回答
  • 2021-02-15 04:39

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

    0 讨论(0)
  • 2021-02-15 04:48

    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
    
    0 讨论(0)
  • 2021-02-15 04:56

    I went around this way

    upload_file = fixture_file_upload('files/stats_upload.csv', 'text/csv')
    upload_file.stubs(:tempfile).returns(upload_file)
    
    0 讨论(0)
提交回复
热议问题