Why does Ruby open-uri's open return a StringIO in my unit test, but a FileIO in my controller?

前端 未结 3 719
梦谈多话
梦谈多话 2020-12-24 06:43

I inherited a Rails 2.2.2 app that stores user-uploaded images on Amazon S3. The attachment_fu-based Photo model offers a rotate method that uses <

3条回答
  •  -上瘾入骨i
    2020-12-24 07:01

    The code responsible for this is in the Buffer class in open-uri. It starts by creating a StringIO object and only creates an actual temp file in the local filesystem when the data exceeds a certain size (10 KB).

    I assume that whatever data your test is loading is small enough to be held in a StringIO and the images you are using in the actual application are large enough to warrant a TempFile. The solution is to use methods which are common to both classes, in particular the read method, with MiniMagick::Image#from_blob:

    temp_image = MiniMagick::Image.from_blob(open(self.public_filename, &:read))
    

提交回复
热议问题