2017-06-15 6 views
0
私はテストのためにRSpecのを使用していると私は工場を使用して、アップロードファイルのためのいくつかのテストを持っている

こんにちはみんなでテストする場合、アップロードきれいな添付ファイルのためのソリューションを必要としています。 は、私は、データベース・クリーナを設定しますが、私はそれが新しいファイルを作成する毎回のパブリックフォルダのツリーを行うときに、私は、私はRSpecのコマンドを実行し、このたびをきれいにしたいです。私はRSpecの

tree public 
public 
├── 404.html 
├── 422.html 
├── 500.html 
├── attachment 
├── favicon.ico 
├── robots.txt 
└── uploads 
    ├── attachment 
    │   └── file 
    │    ├── 1 
    │    │   ├── photo.jpg 
    │    │   ├── photo02.jpg 
    │    │   └── photo03.jpg 
    │    ├── 10 
    │    │   └── photo03.jpg 
    │    ├── 11 
    │    │   └── photo.jpg 
    │    ├── 12 
    │    │   └── photo02.jpg 
    │    ├── 13 
    │    │   └── photo.jpg 
    │    ├── 14 
    │    │   └── photo02.jpg 
    │    ├── 15 
    │    │   └── photo03.jpg 
    │    ├── 16 
    │    │   └── photo.jpg 
    │    ├── 17 
    │    │   └── photo02.jpg 
    │    ├── 18 
    │    │   └── photo.jpg 
    │    ├── 19 
    │    │   └── photo02.jpg 
    │    ├── 2 
    │    │   ├── photo.jpg 
    │    │   └── photo02.jpg 
    │    ├── 20 
    │    │   └── photo03.jpg 
    │    ├── 21 
    │    │   └── photo.jpg 
    │    ├── 22 
    │    │   └── photo02.jpg 
    │    ├── 23 
    │    │   └── photo.jpg 
    │    ├── 24 
    │    │   └── photo02.jpg 
    │    ├── 25 
    │    │   └── photo03.jpg 
    │    ├── 26 
    │    │   └── photo.jpg 
    │    ├── 27 
    │    │   └── photo02.jpg 
    │    ├── 28 
    │    │   └── photo.jpg 
    │    ├── 29 
    │    │   └── photo02.jpg 
    │    ├── 3 
    │    │   ├── photo.jpg 
    │    │   └── photo03.jpg 
    │    ├── 30 
    │    │   └── photo.jpg 
    │    ├── 31 
    │    │   └── photo02.jpg 
    │    ├── 32 
    │    │   └── photo.jpg 
    │    ├── 33 
    │    │   └── photo02.jpg 
    │    ├── 34 
    │    │   └── photo.jpg 
    │    ├── 35 
    │    │   └── photo02.jpg 
    │    ├── 36 
    │    │   └── photo.jpg 
    │    ├── 37 
    │    │   └── photo02.jpg 
    │    ├── 38 
    │    │   └── photo.jpg 
    │    ├── 39 
    │    │   └── photo02.jpg 
    │    ├── 4 
    │    │   └── photo02.jpg 
    │    ├── 40 
    │    │   └── photo.jpg 
    │    ├── 41 
    │    │   └── photo02.jpg 
    │    ├── 42 
    │    │   └── photo.jpg 
    │    ├── 43 
    │    │   └── photo02.jpg 
    │    ├── 44 
    │    │   └── photo.jpg 
    │    ├── 45 
    │    │   └── photo02.jpg 
    │    ├── 46 
    │    │   └── photo.jpg 
    │    ├── 47 
    │    │   └── photo02.jpg 
    │    ├── 48 
    │    │   └── photo.jpg 
    │    ├── 49 
    │    │   └── photo02.jpg 
    │    ├── 5 
    │    │   └── photo03.jpg 
    │    ├── 50 
    │    │   └── photo.jpg 
    │    ├── 51 
    │    │   └── photo02.jpg 
    │    ├── 52 
    │    │   └── photo.jpg 
    │    ├── 53 
    │    │   └── photo02.jpg 
    │    ├── 54 
    │    │   └── photo.jpg 
    │    ├── 55 
    │    │   └── photo02.jpg 
    │    ├── 56 
    │    │   └── photo.jpg 
    │    ├── 57 
    │    │   └── photo02.jpg 
    │    ├── 58 
    │    │   └── photo.jpg 
    │    ├── 59 
    │    │   └── photo02.jpg 
    │    ├── 6 
    │    │   └── photo.jpg 
    │    ├── 60 
    │    │   └── photo.jpg 
    │    ├── 61 
    │    │   └── photo02.jpg 
    │    ├── 62 
    │    │   └── photo.jpg 
    │    ├── 63 
    │    │   └── photo02.jpg 
    │    ├── 64 
    │    │   └── photo.jpg 
    │    ├── 65 
    │    │   └── photo02.jpg 
    │    ├── 66 
    │    │   └── photo.jpg 
    │    ├── 67 
    │    │   └── photo02.jpg 
    │    ├── 68 
    │    │   └── photo.jpg 
    │    ├── 69 
    │    │   └── photo02.jpg 
    │    ├── 7 
    │    │   └── photo02.jpg 
    │    ├── 70 
    │    │   └── photo.jpg 
    │    ├── 71 
    │    │   └── photo02.jpg 
    │    ├── 72 
    │    │   └── photo.jpg 
    │    ├── 73 
    │    │   └── photo02.jpg 
    │    ├── 74 
    │    │   └── photo.jpg 
    │    ├── 75 
    │    │   └── photo02.jpg 
    │    ├── 8 
    │    │   └── photo.jpg 
    │    └── 9 
    │     └── photo02.jpg 
    ├── post 
    └── tmp 

だから、のconfigureのため、私はスペック/サポート/ database_cleaner.rbに入れたデータベース・クリーナ:

RSpec.configure do |config| 
    config.before(:suite) do 
    DatabaseCleaner.strategy = :deletion 
    DatabaseCleaner.clean_with(:deletion) 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 

私rails_helper:attachments.rbの

# This file is copied to spec/ when you run 'rails generate rspec:install' 
require 'spec_helper' 
ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../../config/environment', __FILE__) 
# Prevent database truncation if the environment is production 
abort("The Rails environment is running in production mode!") if Rails.env.production? 
require 'rspec/rails' 
require "pundit/rspec" 


# Add additional requires below this line. Rails is not loaded until this point! 

# Requires supporting ruby files with custom matchers and macros, etc, in 
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 
# run as spec files by default. This means that files in spec/support that end 
# in _spec.rb will both be required and run as specs, causing the specs to be 
# run twice. It is recommended that you do not name files matching this glob to 
# end with _spec.rb. You can configure this pattern with the --pattern 
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 
# 
# The following line is provided for convenience purposes. It has the downside 
# of increasing the boot-up time by auto-requiring all files in the support 
# directory. Alternatively, in the individual `*_spec.rb` files, manually 
# require only the support files necessary. 
# 
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 

# Checks for pending migration and applies them before tests are run. 
# If you are not using ActiveRecord, you can remove this line. 
ActiveRecord::Migration.maintain_test_schema! 

RSpec.configure do |config| 
    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = false 

    # RSpec Rails can automatically mix in different behaviours to your tests 
    # based on their file location, for example enabling you to call `get` and 
    # `post` in specs under `spec/controllers`. 
    # 
    # You can disable this behaviour by removing the line below, and instead 
    # explicitly tag your specs with their type, e.g.: 
    # 
    #  RSpec.describe UsersController, :type => :controller do 
    #  # ... 
    #  end 
    # 
    # The different available types are documented in the features, such as in 
    # https://relishapp.com/rspec/rspec-rails/docs 
    config.infer_spec_type_from_file_location! 

    # Filter lines from Rails gems in backtraces. 
    config.filter_rails_from_backtrace! 
    # arbitrary gems may also be filtered via: 
    # config.filter_gems_from_backtrace("gem name") 

    # Include Factory Girl syntax to simplify calls to factories 
    config.include FactoryGirl::Syntax::Methods 

    Shoulda::Matchers.configure do |config| 
    config.integrate do |with| 
     # Choose a test framework: 
     with.test_framework :rspec 
     # Or, choose the following (which implies all of the above): 
     with.library :rails 
    end 
    end 

    config.include Warden::Test::Helpers, type: :feature 
    config.after(type: :feature) { Warden.test_reset! } 
    config.use_transactional_fixtures = false 
    config.include Devise::Test::ControllerHelpers, type: :controller 
end 

私の工場:

FactoryGirl.define do 
    factory :attachment do 
    transient do 
     file_to_attach "spec/fixtures/photo.jpg" 
    end 

    file { File.open file_to_attach } 
    end 
end 

答えて

0

私はワットウェブ上で探して、これをしようとすると、それは今働いている。 私の場合、設定が理想的だと思います。しかし誰かが同意しない何かを見たら私に知らせてください!

私はconfig/initializers/carrierwave.rb

if Rails.env.test? || Rails.env.cucumber? 

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

    CarrierWave::Uploader::Base.descendants.each do |klass| 
    next if klass.anonymous? 
    klass.class_eval do 
    def cache_dir 
     "#{Rails.root}/public/uploads/tmp" 
    end 

    def store_dir 
     "#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
    end 
end 
end 

CarrierWave.configure do |config| 
    config.asset_host = ActionController::Base.asset_host 
end 

にconfigおよび私のspec_helper.rbに私はこれを追加する:私はRSpecのコマンドを実行した後

config.after(:each) do 
    if Rails.env.test? || Rails.env.cucumber? 
    FileUtils.rm_rf(Dir["#{Rails.root}/public/uploads"]) 
    end 
    end 

は今、公共の私のツリーはこれです:

tree public 
public 
├── 404.html 
├── 422.html 
├── 500.html 
├── attachment 
├── favicon.ico 
└── robots.txt 
テストを作成しました

だから、それは削除されたファイルが、今は別の問題FILに気付きます開発モードで作成されたユーザーは削除され、テストを実行するたびに削除されます。 誰かがこの新しい問題を手伝うことができたら?私は感謝します!

+1

適切にあなたが '"#{} Rails.root /公共/アップロード/設定されているので。あなたの開発環境と同じかもしれない、テスト環境でのアップロードのためのあなたのディレクトリとして ""。代わりに '' {{Rails.root}/spec/support/uploads/...」 'を試してみてください。 –

1

これは私が私の最近のプロジェクトでそれについていった方法です:config/initializers/carrierwave.rb

if Rails.env.test? 
    Dir["#{Rails.root}/app/uploaders/*.rb"].each { |file| require file } 

    CarrierWave::Uploader::Base.descendants.each do |klass| 
    next if klass.anonymous? 
    klass.class_eval do 
     def cache_dir 
     "#{Rails.root}/spec/support/uploads/tmp" 
     end 

     def store_dir 
     "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
     end 
    end 
    end 
end 

spec/spec_helper.rb

config.after(:each) do 
    FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"]) 
end 
+0

こんにちはDaniel Friss、まずはお返事ありがとうございます!そして私はあなたの設定でやり直しています。私がテストするとエラーが出ます。これは、私が変更したフォルダだと思います。Look: 'Adminsは複数の添付ファイルを含む投稿を作成することができます Failure/Error:ActionController :: RoutingErrorを発生させます[#{env ['REQUEST_METHOD']}]#{env ['PATH_INFO']。inspect}"ActionController :: RoutingError: ルートが[GET]と一致しません "/Users/romenigld/workspace/projects/news_city/spec/support/uploads/attachment/file/7/photo.jpg" – rld

0

に、私は別の事をやっていたし、私はで

このプロジェクトをコーディングして、今度は、モードテストとdevのための別のパスにアップロードするためのセットアップをしました。これが解決策でした。そしてコードを共有します。私はこれを投稿しています。 そして、ありがとうございましたDaniel Friisさん。

のconfig// carrerwave.rb

if Rails.env.test? || Rails.env.cucumber? 
    CarrierWave.configure do |config| 
    config.storage = :file 
    config.enable_processing = false 
    end 

    # make sure our uploader is auto-loaded 
    AttachmentUploader 

    # use different dirs when testing 
    CarrierWave::Uploader::Base.descendants.each do |klass| 
    next if klass.anonymous? 
    klass.class_eval do 
     def cache_dir 
     "#{Rails.root}/spec/support/uploads/tmp" 
     end 

     def store_dir 
     "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
     end 
    end 
    end 
end 

CarrierWave.configure do |config| 
    config.asset_host = ActionController::Base.asset_host 
end 

及び仕様/ rails_helperに置くを初期化します。

# Clean up uploaded files 
    config.after(:each) do 
    if Rails.env.test? || Rails.env.cucumber? 
     FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"]) 
    end 
    end 
関連する問題