0
我有我的Report
型號:Rspec的:Carrierwave不保存從規格/夾具文件
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
梅託德,我用它來保存文檔和其他PARAM。現在我想測試這個函數的工作原理。
/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"
),然後我的測試通過。
我該如何解決這個問題?