FactoryGirl / Bot图像定义

问题描述 投票:2回答:1

我有一个功能测试。在此功能测试中,我需要将一些数据加载到测试数据库中:let!(:product1) { create(:product) }

我正在使用FactoryBot创建并将:product对象保存到数据库:

FactoryBot.define do
  factory :product do
    name 'Master Coat'
    color 'black'
    size 'M'
    price '870'
    available true
    order 1
    image_1 '/assets/images/test_image.jpeg'
    image_2 '/assets/images/test_image.jpeg'
    image_3 '/assets/images/test_image.jpeg'
    image_4 '/assets/images/test_image.jpeg'
    image_5 '/assets/images/test_image.jpeg'
    image_6 '/assets/images/test_image.jpeg'
  end
end

我运行我的RSpec测试:

  describe 'GET #detail' do
    it 'should return only products with a specified name' do
        visit detail_path(name: 'Master Coat')
    end
  end

但是,我得到一个错误,因为我的视图需要生成图像1,2等,这在我的真实应用程序中通过URL从AWS加载。但在我的测试中,我不想向AWS发送请求,因为我没有测试图像。加载的图像无关紧要。我只想在我的app/assets/images/test_image.jpeg目录中有一个虚拟图像,并让FactroyBot使用该图像进行所有测试,但是我无法使其工作,因为我错误地指定了图像的路径,而FactoryBot给出了一个错误:

ActionView::Template::Error:
       The asset "assets/images/test_image.jpeg" is not present in the asset pipeline.

我尝试了几种不同的方法让它发挥作用。什么是正确的方式来指定我的图像文件夹的URL,以便FactoryBot可以检索虚拟图像?这是最好的方式,还是更好的方式?

ruby-on-rails rspec factory-bot factory
1个回答
2
投票

只需删除路径即可。当你使用image_tag('test_image.jpeg') rails时会看到app/assets/images。您也可以将文件放在公共目录中,它们将以静态方式提供,而不是通过管道。

FactoryBot.define do
  factory :product do
    name 'Master Coat'
    color 'black'
    size 'M'
    price '870'
    available true
    order 1
    (1..6).each {|n| send "image_#{n}", 'test_image.jpeg' }
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.