Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 测试ActiveStorage附件(FileNotFound)_Ruby On Rails_Rails Activestorage - Fatal编程技术网

Ruby on rails 测试ActiveStorage附件(FileNotFound)

Ruby on rails 测试ActiveStorage附件(FileNotFound),ruby-on-rails,rails-activestorage,Ruby On Rails,Rails Activestorage,我在测试ActiveStorage附件时出错。代码如下所示: class AssemblyTest

我在测试ActiveStorage附件时出错。代码如下所示:

class AssemblyTest
我得到以下错误
Minitest::意外错误:ActiveStorage::FileNotFoundError:ActiveStorage::FileNotFoundError
调用@assembly.image.download时。附加的
断言正在通过,但我不明白文件下载失败的原因。另外,在
tmp/storage
目录中没有显示任何内容,ActiveStorage被配置为存储文件。

试试这个

@assembly = Assembly.create(name: assemblies(:head_gasket).name)
@assembly.image.attach(io: File.open('/path/to/file'), filename: 'file.name', content_type: 'mime/type')

在挖掘依赖于实际数据库的ActiveStorage代码以执行文档上载(或保存到光盘)时:

在提交后(在:%i[create update]上){attachment\u changes.delete(name.to\s)。尝试(:upload)}
如果在测试环境中使用数据库事务,则不会存储文档

要解决此问题,可以手动触发提交回调:

run\u回调(:commit)
因此,在您的情况下,这可能会起作用:

class AssemblyTest
我认为您不需要测试activestorage是否有效。我不是在测试activestorage是否有效。我正在测试一个依赖于activestorage中存储的文件的方法。抱歉,我发布了一段实际发生的事情,以便于理解/隔离。我实际上在测试另一种方法,它依赖于存储在activestorage中的文件。