我有一个rspec测试的问题 . 一切似乎都在我的localhost中工作,但我的测试仍然失败:
失败/错误:new_file.content_type.start_with? “应用/ PDF”
NoMethodError:nil的未定义方法'content_type':NillClass
RSpec file
# frozen_string)_literal: true
require "rails_helper"
RSpec.describe DocumentLibraryService do
context "sorts documents" do
it "sorts and returns only the 6 most recent" do
developer = create(:developer)
document1 = build(:document, title: "Developer document 2", file: "Document2.pdf", documentable: developer)
document2 = build(:document, title: "Developer document 5", file: "Document5.pdf", documentable: developer)
document3 = build(:document, title: "Developer document 7", file: "Document7.pdf", documentable: developer)
document4 = build(:document, title: "Developer document 8", file: "Document8.pdf", documentable: developer)
document5 = build(:document, title: "Developer document 9", file: "Document9.pdf", documentable: developer)
document6 = build(:document, title: "Developer document 20", file: "Document9.pdf", documentable: developer)
document7 = build(:document, title: "Developer document 4", file: "Document9.pdf", documentable: developer)
documents = []
documents.push(document1, document2, document3, document4, document5, document6, document7)
result = described_class.call(documents, [])
expect(result.count).to eq(6)
expect(result[0][:name]).to eq(document7.title)
expect(result[1][:name]).to eq(document6.title)
expect(result[2][:name]).to eq(document5.title)
expect(result[3][:name]).to eq(document4.title)
expect(result[4][:name]).to eq(document3.title)
expect(result[5][:name]).to eq(document2.title)
end
end
end
失败是引用上传者的一部分:
version :preview, if: :not_svg? do
process convert: :jpg
def full_filename(for_file = model.file)
super.chomp(File.extname(super)) + ".jpg"
end
def type_pdf?
pdf?(file)
end
protected
def not_svg?(new_file)
!new_file.content_type.start_with? "image/svg+xml"
!new_file.content_type.start_with? "application/pdf"
end
def pdf?(new_file)
new_file.content_type.start_with? "application/pdf"
end
在以下上下文中从两个地方调用:
def library_preview_url(file)
if file.type_pdf?
return("pdf_icon.jpg")
else
return file.preview.url(response_content_type: %( "image/jpeg" )) if file.preview.present?
file.url(response_content_type: %( "image/svg+xml"))
end
end
正如我之前提到的,一切都在我的localhost中按预期工作,我无法看到是什么让这个测试失败 .
引用pdf文件的所有部分都是新添加的部分;在这些被添加之前,测试正在通过,这已经缩小了(我相信?)到调用type_pdf的问题?