[英]Defining fabricator w/ file attachment & testing file upload w/ rspec
I'm trying to figure out the how to test file attachments w/ the Fabrication and Rspec gems. 我正在试图弄清楚如何使用Fabrication和Rspec宝石测试文件附件。 The file upload works fine when testing the site manually, there's just no Rspec coverage.
手动测试站点时文件上传工作正常,没有Rspec覆盖。 The problem seems to be that I don't know how to include an attachment in a PUT request.
问题似乎是我不知道如何在PUT请求中包含附件。
How do I add a file attachment, preferably using a fabricator, to this test 如何在此测试中添加文件附件(最好使用制造商)
Fabricator: 制造商:
Fabricator(:application) do
email Faker::Internet.email
name Faker::Name.name
resume_url { File.open(
File.join(
Rails.root,
"spec",
"support",
"files",
"hey_look_a_pdf_pdf_lolz.pdf"
)
)
}
end
Controller: 控制器:
class ApplicationsController < ApplicationController
def update
@application = Application.find_or_initialize_by(id: params[:id])
if @application.update(application_params)
flash[:success] = "Application has been saved :)"
redirect_to application_path(@application)
else
render :edit
end
end
private
def application_params
params[:application].permit(:email, :job_id, :name, :resume_url)
end
end
Controller test 控制器测试
require "spec_helper"
# this is a sample application attributes being passed into controller
# it should have a file attachment, but haven't figured out how to do that
#
# {
# "id"=>"fa446fdf-b82d-48c0-8979-cbbb2de4fb47",
# "email"=>"old@example.com",
# "name"=>"Dr. Rebeca Dach",
# "resume_url"=>nil,
# "job_id"=>"cf66dbcf-d110-42cc-889b-0b5ceeeed239",
# "created_at"=>nil,
# "updated_at"=>nil
# }
describe ApplicationsController do
context "PUT /applications/:id" do
it "creates an application" do
expect { put(
:update,
application: application.attributes,
id: application.id
)}.to change{Application.count}.from(0).to(1)
end
end
end
The following Fabricator seems to work okay. 以下Fabricator似乎工作正常。 I'm still not able to get file attachments to work in controller tests.
我仍然无法在控制器测试中使用文件附件。
Fabricator(:application) do
email Faker::Internet.email
name Faker::Name.name
resume_url { ActionDispatch::Http::UploadedFile.new(
tempfile: File.new(Rails.root.join(
"./spec/support/files/hey_look_a_pdf_pdf_lolz.pdf"
)),
filename: File.basename(File.new(Rails.root.join(
"./spec/support/files/hey_look_a_pdf_pdf_lolz.pdf"
)))
)}
end
OK, I got it figured out. 好的,我明白了。 There were two pieces.
有两件。
1) I had to fix the Fabricator, which I mentioned in "Update #1" to my question. 1)我必须修改我在“更新#1”中提到的Fabricator来解决我的问题。 Here's a simpler format for the Fabricator using Rack::Test::Upload.new
这是使用Rack :: Test :: Upload.new的Fabricator的一种更简单的格式
Fabricator(:application) do
email Faker::Internet.email
name Faker::Name.name
resume_url {
Rack::Test::UploadedFile.new(
"./spec/support/files/hey_look_a_pdf_pdf_lolz.pdf",
"application/pdf"
)
}
end
2) I was using Fabricator.build(:application).attributes
, which wasn't compatible with a file attachment. 2)我使用的是
Fabricator.build(:application).attributes
,它与文件附件不兼容。 Instead, I started using Fabricator.attributes_for(:application)
, and everything started working great. 相反,我开始使用
Fabricator.attributes_for(:application)
,一切都开始运作良好。 Here's the passing. 这是过客。
describe ApplicationsController do
context "PUT /applications/:id" do
let(:job) { Fabricate(:job) }
let(:application) do
Fabricate.attributes_for(
:application,
email: "old@example.com",
id: SecureRandom.uuid,
job_id: job.id
)
end
it "creates an application" do
expect { put(
:update,
application: application,
id: application["id"]
)}.to change{Application.count}.from(0).to(1)
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.