文件格式验证在测试期间未通过
Validate for file format doesn't pass during tests
我有模型 ImportFile
,它有 has_one_attached :document
并验证文件格式:
validates :document, blob: { content_type: ['application/vnd.ms-excel'] }
(仅限 .xls)
我为此模型创建了工厂:
FactoryBot.define do
factory :import_file do
association :user
processed { false }
document { Rack::Test::UploadedFile.new('spec/fixtures/orders.xls') }
end
end
当我 运行 测试时,它们没有通过,我收到验证错误:
Validation failed: Document is not a valid file format
但是,如果我在网站上加载此文件,它会完美运行。
为什么格式验证在测试期间未通过?
FactoryBot.define do
factory :import_file do
association :user
processed { false }
document do
Rack::Test::UploadedFile.new(
'spec/fixtures/orders.xls',
content_type: 'application/vnd.ms-excel'
)
end
end
end
Rack::Test::UploadedFile
实际上并没有尝试从文件中推断出 MIME 类型——它只是默认为 'text/plain'.
我有模型 ImportFile
,它有 has_one_attached :document
并验证文件格式:
validates :document, blob: { content_type: ['application/vnd.ms-excel'] }
(仅限 .xls)
我为此模型创建了工厂:
FactoryBot.define do
factory :import_file do
association :user
processed { false }
document { Rack::Test::UploadedFile.new('spec/fixtures/orders.xls') }
end
end
当我 运行 测试时,它们没有通过,我收到验证错误:
Validation failed: Document is not a valid file format
但是,如果我在网站上加载此文件,它会完美运行。
为什么格式验证在测试期间未通过?
FactoryBot.define do
factory :import_file do
association :user
processed { false }
document do
Rack::Test::UploadedFile.new(
'spec/fixtures/orders.xls',
content_type: 'application/vnd.ms-excel'
)
end
end
end
Rack::Test::UploadedFile
实际上并没有尝试从文件中推断出 MIME 类型——它只是默认为 'text/plain'.