2013-10-11 65 views
0

我有一個簡單的測試來檢查carrierwave上傳器的工作原理。使用`testrb`測試通過,但使用Rails`rake test`崩潰?

爲此,我使用minitest,測試在獨立運行時運行,但不在Rails的rake test...環境下運行。

(代碼爲測試包含下面。)

的事物,它們:

  • 如果我運行ruby test/uploaders/image_file_uploader_test.rb測試通過。
  • 如果我運行testrb test/uploaders/image_file_uploader_test.rb測試通過。
  • 如果我手動調用從IRB測試,代碼沒有什麼預期
  • 如果我創建了一個小耙TestTask運行文件(任務也低於拷貝)的所有行( Rails的控制檯)測試通過。

東西不起作用:

  • 如果我打電話rake test test/uploaders/image_file_uploader_test.rb我掉下進入調試器(堆棧跟蹤下文)
  • 如果我打電話zeus rake test test/uploaders/image_file_uploader_test.rb我也掉下進入調試

奇怪的是:

如果我將調試程序中的調用堆棧擡頭到引起失敗的測試行 - uploader.store!(@file) - 並直接使用(rdb:1) p uploader.store!(@file)調用它,它將起作用!我的意思是,該方法按預期返回,並且文件出現在正確的目錄中。

想法?

我可能會在這裏做一些非常愚蠢的事情。它必須與加載Rails環境的Rake任務有關,對吧?我是雙重的 - 包括東西或其他東西?這與Rails環境中的初始化工具有關嗎? (N.B.,當我從調試器執行store!時,它將文件存儲在測試中指定的位置,因此它成功覆蓋了Rails初始化程序)。

的回溯

bundle exec rake test test/uploaders/*_test.rb

# Running tests: 

/Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75: `' (NilClass) 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:926:in `_run_suite' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `block in _run_suites' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `map' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1072:in `block in _run' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1071:in `each' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1071:in `_run' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:795:in `block in autorun' 
/Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75: 
(rdb:1) where 
--> #1 /Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75:in `rmdir' 
    #2 /Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:61:in `with_callbacks' 
    #3 /Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:58:in `store!' 
    #4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file' 
    #5 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1258:in `run' 
    #6 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:933:in `_run_suite' 
    #7 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites' 
    #8 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything' 
    #9 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests' 
    #10 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1072:in `_run' 
    #11 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run' 
(rdb:1) up 3 
#4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file' 
(rdb:1) list 
[33, 42] in /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb 
    33 end 
    34 
    35 # The whole point of this is to upload a file. 
    36 def test_upload_of_file 
    37  uploader = ImageFileUploader.new 
=> 38  uploader.store!(@file) 
    39  # Ensure the uploaded file is correct. 
    40  assert_equal Digest::SHA2.file(@file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest 
    41 end 
    42 
(rdb:1) p uploader.store!(@file) 
[:store_versions!] 
(rdb:1) 

p uploader.store!(@file)的號召,文件已保存

後從zeus test test/uploaders/image_file_uploader_test.rb(這是非常類似上面)

/Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75: `' (NilClass) 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (3 levels) in go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `fork' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (2 levels) in go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `each' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (3 levels) in go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `fork' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (2 levels) in go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `each' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop' 
    from /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `go' 
    from -e:1:in `<main>' 
/Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75: 
(rdb:1) where 
--> #1 /Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75:in `rmdir' 
    #2 /Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:61:in `with_callbacks' 
    #3 /Users/leo/.rvm/gems/[email protected]_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:58:in `store!' 
    #4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file' 
    #5 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1258:in `run' 
    #6 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:933:in `_run_suite' 
    #7 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites' 
    #8 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything' 
    #9 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests' 
    #10 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1072:in `_run' 
    #11 /Users/leo/.rvm/gems/[email protected]_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run' 
    #12 /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus/m.rb:203:in `execute' 
    #13 /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus/m.rb:121:in `run' 
    #14 /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus/m.rb:106:in `run' 
    #15 /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus/rails.rb:190:in `test' 
    #16 /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:116:in `command' 
    #17 /Users/leo/.rvm/gems/[email protected]_site/gems/zeus-0.13.3/lib/zeus.rb:80:in `go' 
(rdb:1) up 3 
#4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file' 
(rdb:1) list 
[33, 42] in /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb 
    33 end 
    34 
    35 # The whole point of this is to upload a file. 
    36 def test_upload_of_file 
    37  uploader = ImageFileUploader.new 
=> 38  uploader.store!(@file) 
    39  # Ensure the uploaded file is correct. 
    40  assert_equal Digest::SHA2.file(@file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest 
    41 end 
    42 
(rdb:1) p uploader.store!(@file) 
[:store_versions!] 
(rdb:1) 

代碼

類被測試

class ImageFileUploader < CarrierWave::Uploader::Base 
    include CarrierWave::RMagick 

    version :thumbnail do 
    process resize_to_fill: [100,100] 
    end 

    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 
end 

測試本身

require 'minitest/autorun' 
require 'minitest/pride' 
require 'minitest/debugger' if ENV['DEBUG'] 
require 'rmagick' 
require 'carrierwave' 
require_relative '../../app/uploaders/image_file_uploader' 

class ImageFileUploaderTest < MiniTest::Unit::TestCase 

    # Before any tests run, set up parameters. 
    FILENAME = 'test_photo_1.jpg' 
    STORE_DIR = 'tmp/uploads/store' 
    CACHE_DIR = 'tmp/uploads/cache' 
    STORE_PATH = File.join __dir__, '..', '..', STORE_DIR 
    CACHE_PATH = File.join __dir__, '..', '..', CACHE_DIR 

    # Override the store and cache dirs so we’re not reliant on Rails. 
    class ::ImageFileUploader 
    storage :file 
    store_dir STORE_PATH 
    cache_dir CACHE_PATH 
    end 

    # Before each test runs, set up a file to test with. 
    def setup 
    @file = File.new "#{__dir__}/../test_files/#{FILENAME}" 
    end 

    # After each test runs, clear the results directory so it doesn't influence other tests. 
    def teardown 
    FileUtils.rm_rf STORE_PATH 
    FileUtils.rm_rf CACHE_PATH 
    end 

    # The whole point of this is to upload a file. 
    def test_upload_of_file 
    uploader = ImageFileUploader.new 
    uploader.store!(@file) 
    # Ensure the uploaded file is correct. 
    assert_equal Digest::SHA2.file(@file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest 
    end 

end 

簡單Rake任務

require 'rake/testtask' 

Rake::TestTask.new('dev:test') do |t| 
    t.test_files = FileList['test/uploaders/*_test.rb'] 
end 

Rails初始化器(N.B.我認爲測試從此隔絕...)

CarrierWave.configure do |config| 
    config.storage = :file 

    # Override the directory where uploaded files will be stored. 
    config.store_dir = -> do 
    if model.nil? 
     "uploads/other/#{Time.now.strftime("%F")}/#{Time.now.strftime("%H-%M-%S")}" 
    else 
     # This is a sensible default for uploaders that are meant to be mounted: 
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
    end 

    # Override the directory where temp files will be stored. 
    config.cache_dir = -> do 
    # This is a better default because it prevents temp files from becoming public and is more consistent with the Rails directory structure. 
    Rails.root.join('tmp/uploads') 
    end 
end 

謝謝!

回答

0

我已經解決了這個問題,雖然沒有通過一個完全科學的過程,因爲我正在改變代碼中的其他內容,包括整個Rails環境。

當前的工作版本極大地簡化了Carrierwave初始化器,將路徑定義移動到上傳器,然後在測試中猴子修補這些方法。令人煩惱的是,基於初始化器的方法的原始原因是自定義目錄被拾取爲原始圖像,但不是處理版本。這似乎現在工作得很好,但是這個當前的代碼與舊的代碼有什麼不同,我不確定。

Rails的初始化

CarrierWave.configure do |config| 
    config.storage = :file 
end 

上傳

class ImageFileUploader < CarrierWave::Uploader::Base 
    include CarrierWave::RMagick 

    version :thumbnail do 
    process resize_to_fill: [100,100] 
    end 

    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 

    def store_dir 
    if model.nil? 
     "uploads/other/#{Time.now.strftime("%F")}/#{Time.now.strftime("%H-%M-%S")}" 
    else 
     # This is a sensible default for uploaders that are meant to be mounted: 
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
    end 

    def cache_dir 
    # This is a better default because it prevents temp files from becoming public and is more consistent with the Rails directory structure. 
    Rails.root.join('tmp/uploads') 
    end 

end 

測試

require_relative '../test_helper' 
require 'rmagick' 
require 'carrierwave' 
require_relative '../../app/uploaders/image_file_uploader' 

class ImageFileUploaderTest < MiniTest::Unit::TestCase 

    # Before any tests run, set up parameters. 
    FILENAME = 'test_photo_1.jpg' 
    STORE_DIR = 'tmp/uploads/store' 
    CACHE_DIR = 'tmp/uploads/cache' 
    STORE_PATH = File.join __dir__, '..', '..', STORE_DIR 
    CACHE_PATH = File.join __dir__, '..', '..', CACHE_DIR 

    # Override the store and cache dirs so we’re not reliant on Rails. 
    class ::ImageFileUploader 
    storage :file 
    def store_dir; STORE_PATH; end 
    def cache_dir; CACHE_PATH; end 
    end 

    # Before each test runs, set up a file to test with. 
    def setup 
    @file = File.new "#{__dir__}/../test_files/#{FILENAME}" 
    end 

    # After each test runs, clear the results directory so it doesn't influence other tests. 
    def teardown 
    FileUtils.rm_rf STORE_PATH 
    FileUtils.rm_rf CACHE_PATH 
    end 

    # The whole point of this is to upload a file. 
    def test_upload_of_file 
    uploader = ImageFileUploader.new 
    uploader.store!(@file) 
    # Ensure the uploaded file is correct. 
    assert_equal Digest::SHA2.file(@file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest 
    end 

    # After each test, there is nothing to clean up, as teardown clears the output dir. 
    def after_tests 
    end 

end 

(注:列入的test_helper是不相關的修復 - 我有它的工作後,我搬到require的一些幫助乾燥與其他測試一些重疊。)

相關問題