gpt4 book ai didi

ruby-on-rails - FactoryGirl 为 rspec 中的对象调用 `original_filename`

转载 作者:行者123 更新时间:2023-12-04 05:12:19 25 4
gpt4 key购买 nike

我正在使用 Documents 类,试图对其进行测试。我定义了以下工厂:

require 'factory_girl'

FactoryGirl.define do
factory :document do
user_id '6315'
name 'Test doc'
description 'W9'
filename 'test_doc.pdf'
filetype 'file'
filesize 500
end

factory :invalid_doc, parent: :document do
filesize 5242900
end
end

使用以下辅助方法访问测试中的正确属性:
def build_attributes(*args)
attrs = FactoryGirl.build(*args).attributes
attrs.delete_if do |k, v|
["id", "created_at", "updated_at"].member?(k)
end
paramify_values(attrs)
end

在每次测试之前,我运行:
before(:each) do
login_as_admin
@doc = @user.documents.create(FactoryGirl.attributes_for(:document))
end

其中@user 设置在 login_as_admin 中宏。在我的测试中,我正在运行:
describe 'POST #create' do
it "should create a new document" do
expect{
post :create, document: build_attributes(:document, user_id: @doc.user_id)
}.to change(Document,:count).by(1)
end

it "should find the right user" do
post :create, document: build_attributes(:document, user_id: @doc.user_id)
assigns(:user).should eq(@user)
end

# some other tests...
end

前一个测试是在 this article 上建议的,后者正是我认为应该发生的事情。 Controller 操作正在为实例分配以下内容:
@user = User.find(document[:user_id])

所以,很标准。但是,这两个测试都抛出相同的错误,
Failure/Error: post :create, document: build_attributes(:document, user_id: @doc.user_id)
NoMethodError:
undefined method `original_filename' for nil:NilClass

但我从不显式调用该方法,所以它是 FactoryGirl 调用的东西吗?该模型描述如下:
attr_accessible :description, :filename, :filesize, :filetype, :name, :user_id

哪里 :filename只是一个字符串。这里可能出了什么问题?我没有使用回形针上传文件,只是一个 file_field在 View 中。我获取路径并将文件保存到 Controller 中的生产服务器,但从不调用此方法。

编辑:

我想实际的 Controller 描述可能会有所帮助哈哈
def create
uploaded_file = params[:document][:file]
document = params[:document]
document.delete(:file)
@user = User.find(document[:user_id])
filepath = Rails.root.join('documents', @user.company_id.to_s, @user.id.to_s, uploaded_file.original_filename)
%x[ mkdir #{Rails.root.join('documents', @user.company_id.to_s)} ]
%x[ mkdir #{Rails.root.join('documents', @user.company_id.to_s, @user.id.to_s)} ]
File.open(filepath, 'wb') do |file|
file.write(uploaded_file.read)
end
document[:filesize]= File.size(filepath)
document[:filetype]= File.ftype(filepath)
document[:filename] = uploaded_file.original_filename
d =Document.new(document)
d.save
redirect_to :action => 'show', :id => user.id
end

请记住我确定有 许多 这种方法有问题。我正在尝试重构它并进行测试。目前,我要做的就是克服第一次打嗝, original_filename方法在某处被调用,我自己没有定义它。谁能看到为什么/在哪里?

最佳答案

original_filename是上传文件的方法,see the rack documentation .
filepath = Rails.root.join('documents', @user.company_id.to_s, @user.id.to_s, uploaded_file.original_filename)

document[:filename] = uploaded_file.original_filename
在 Controller 中获取原始文件名,因为当文件被上传时,它会获得一个丑陋的临时文件名进行存储,您希望使用原始文件名使其可读和准确。

考虑使用 fixture_file_upload rspec 中的 helper 。这是一个示例规范:

    expect {
post :create, document: attributes_for(:document, user_id: @doc.user_id, file: fixture_file_upload('spec/assets/documents/test_doc.pdf', 'appliation/pdf'))
}.to change(Document, :count).by(1)

并在 spec/assets/documents/test_doc.pdf 中放置一个测试 pdf

关于ruby-on-rails - FactoryGirl 为 rspec 中的对象调用 `original_filename`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14760914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com