我有我的Report
模型:
class Report < ActiveRecord::Base
belongs_to :user
attr_accessible :ready_status, :document
mount_uploader :document, DocumentUploader
def attach( report_file )
self.update_attributes( :document => File.open( report_file ), :ready_status => true )
end
end
这个模型有attach
方法,我用它来保存文档和其他参数。现在我想测试这个功能是否有效。
/spec/models/report_spec.rb
# encoding: utf-8
require 'spec_helper'
describe Report do
before(:each) do
@user = User.make!
end
...
context "File's saving" do
before(:each) do
@report = @user.reports.create
@csv_report_file = "#{Rails.root}/spec/files/report.csv"
end
it "CSV should be saved" do
csv_report_filename = @csv_report_file.split("/").last
@report.attach @csv_report_file
@report.reload
@report.document.file.filename.should == csv_report_filename
end
end
end
当我尝试从中保存文件时,/spec/files
出现这样的错误:
Report File's saving CSV should be saved
Failure/Error: @report.document.file.filename.should == csv_report_filename
NoMethodError:
undefined method `filename' for nil:NilClass
但是当我从另一个文件夹(例如"#{Rails.root}/samples/my-report.csv"
)尝试另一个文件时,我的测试通过了。
我该如何解决?