Ruby on rails 从RSpec测试到资产文件夹的路径
我正在尝试使用RSpec/capybara/Factory girl for Rails测试一个carrierwave图像上传到一个模型。Ruby on rails 从RSpec测试到资产文件夹的路径,ruby-on-rails,ruby,rspec,capybara,carrierwave,Ruby On Rails,Ruby,Rspec,Capybara,Carrierwave,我正在尝试使用RSpec/capybara/Factory girl for Rails测试一个carrierwave图像上传到一个模型。 此特定测试测试是否应显示图像 目前,我有以下代码: it "should accept a spotlight record with spotlight info" do feature = create :feature, spotlight: true, spotlight_description: "description", spotligh
此特定测试测试是否应显示图像 目前,我有以下代码:
it "should accept a spotlight record with spotlight info" do
feature = create :feature, spotlight: true, spotlight_description: "description", spotlight_image: File.open(Rails.root.join "/app/assets/shopstar_logo_stamp.png")
expect(feature).to be_valid
end
但不知怎的,图像没有被检测到,我得到了这个错误:
Failures:
1) Feature validations should accept a spotlight record with spotlight info
Failure/Error: feature = create :feature, spotlight: true, spotlight_description: "description", spotlight_image: File.open(Rails.root.join "/app/assets/shopstar_logo_stamp.png")
Errno::ENOENT:
No such file or directory - /app/assets/shopstar_logo_stamp.png
# ./spec/models/feature_spec.rb:32:in `initialize'
# ./spec/models/feature_spec.rb:32:in `open'
# ./spec/models/feature_spec.rb:32:in `block (3 levels) in <top (required)>'
故障:
1) 功能验证应接受具有聚光灯信息的聚光灯记录
失败/错误:feature=create:feature,spotlight:true,spotlight\u description:“description”,spotlight\u image:File.open(Rails.root.join)/app/assets/shopstar\u logo\u stamp.png)
Errno::enoint:
没有此类文件或目录-/app/assets/shopstar\u logo\u stamp.png
#./spec/models/feature_spec.rb:32:in'initialize'
#./spec/models/feature_spec.rb:32:in'open'
#./spec/models/feature_spec.rb:32:in'block(3层)in'
如何在资产中指定图像的路径并将其用于测试?
或者,测试carrierwave图像上传的更好方法是什么?如果这是一个验收测试/集成测试,您实际上希望从用户角度使用水豚这样做:
feature 'user uploads image' do
scenario '#Image' do
count = ImageModel.count
visit new_image_path
attach_file('css_selector_here', File.join(Rails.root, '/spec/support/herst.jpg'))
click_button('Submit')
expect(page).to have_content('Image uploaded successfully!')
expect(ImageModel.count).to eq(count + 1)
end
end
如果您正在进行单元测试,请在spec/factories/factory.rb
Factory.define :feature do |f|
f.spotlight true
f.spotlight_description "description"
f.spotlight_image { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'feature', 'images', 'shopstar_logo_stamp.jpg')) }
end
现在,在单元测试中,您可以运行测试:
it "should accept a spotlight record with spotlight info" do
feature = create :feature
expect(feature).to be_valid
end
这实际上是一个模型测试。对不起,如果我的问题不清楚的话。。我是测试新手嗨,马可-我的回答对你有帮助吗?如果是,请接受。