0
當我嘗試在測試中使用自定義插值時失敗。然而,萬物在開發環境中工作良好,並且測試在沒有自定義插值+ 的情況下工作,有時它們與自定義插值一起工作。Rails回形針自定義內插錯誤測試
我的代碼:
class ActiveSupport::TestCase
fixtures :all
def file_fixture(filename = "sample_file.png")
File.new("test/fixtures/documents/#{filename}")
end
end
test 'document attachment must be from valid file extension' do
document = Document.new
document.appeal_id = Appeal.first.id
document.attachment = file_fixture('FailTest - bad filename extension.txt')
assert_not document.valid?, 'Document attachment should not be TXT'
document.attachment = file_fixture('Test - medical.pdf')
assert document.valid?, 'Document attachment with pdf extension should be valid'
end
application.rb中:
Paperclip.interpolates :year do |attachment, style|
attachment.instance.created_at.year
end
Paperclip.interpolates :month do |attachment, style|
attachment.instance.created_at.month
end
Paperclip.interpolates :appeal_id do |attachment, style|
attachment.instance.appeal.id
end
Paperclip.interpolates :env do |attachment, style|
Rails.env
end
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
Paperclip::Attachment.default_options[:default_url] = '/images/missing.jpg'
Paperclip::Attachment.default_options[:path] = ':rails_root/public/attachments/:env/:year/:month/:appeal_id/:hash.:extension'
Paperclip::Attachment.default_options[:url] = '/attachments/:env/:year/:month/:appeal_id/:hash.:extension'
我得到的錯誤是:
Minitest::UnexpectedError: NoMethodError: undefined method `year' for nil:NilClass
config/application.rb:27:in `block in <class:Application>'
test/models/document_test.rb:43:in `block in <class:DocumentTest>'
,這是因爲在:year
插值created_at
解析nil
我的問題: 它爲什麼只解決了測試環境而不是所有的時間? (另一個測試成功地將文件添加到使用日期調度的路徑)
您。嘗試(:一年)的代碼工作。我認爲它不會因爲驗證問題而被保存,因此在created_at中沒有任何內容。 ...需要更多地瞭解它,反正 - 謝謝! – yossico
是的,我認爲用'try'是最合適的解決方案。順便說一句,它是用':year'等於'nil'來插入它的嗎? – Aleksey