簡體   English   中英

捆綁exec rake規范和自定義rake任務

[英]Bundle exec rake spec and custom rake tasks

我有一個簡單的rake任務,專門用於填充我的postgresql數據庫(dev,test和prod環境的相同db):

# lib/tasks.sample_data.rake
namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    make_users
    make_user_apps
  end
end

def make_users
 ... users = []; users << User.create(...)
end
........ 

當我使用'bundle exec rake db:populate'這個任務工作正常。 我有完整的用戶記錄數據庫。

我也有一些使用模型User的rspec測試,以及命令問題

bundle exec rake spec

這個命令由於某種原因破壞了我的數據庫,我有以下測試結果:

Failures:

  1) Lead should return counters hash
     Failure/Error: user = User.find(1)
     ActiveRecord::RecordNotFound:
       Couldn't find User with id=1
     # ./spec/models/lead_spec.rb:23:in `block (2 levels) in <top (required)>'

Finished in 0.16812 seconds

執行此rake任務后,我可以看到User.count返回0

我有默認的Rakefile,這里是我所有rake任務的列表:

rake -T

rake about                              # List versions of all Rails frameworks and the environment
rake assets:clean[keep]                 # Remove old compiled assets
rake assets:clobber                     # Remove compiled assets
rake assets:environment                 # Load asset compile environment
rake assets:precompile                  # Compile all the assets named in config.assets.precompile
rake cache_digests:dependencies         # Lookup first-level dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake cache_digests:nested_dependencies  # Lookup nested dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake db:create                          # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop                            # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load                   # Load fixtures into the current environment's database
rake db:migrate                         # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:status                  # Display status of migrations
rake db:populate                        # Fill database with sample data
rake db:rollback                        # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear              # Clear a db/schema_cache.dump file
rake db:schema:cache:dump               # Create a db/schema_cache.dump file
rake db:schema:dump                     # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load                     # Load a schema.rb file into the database
rake db:seed                            # Load the seed data from db/seeds.rb
rake db:setup                           # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump                  # Dump the database structure to db/structure.sql
rake db:version                         # Retrieves the current schema version number
rake doc:app                            # Generate docs for the app -- also available doc:rails, doc:guides (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake log:clear                          # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
rake middleware                         # Prints out your Rack middleware stack
rake notes                              # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom                       # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template                     # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update                       # Update configs and some other initially generated files (or use just update:configs, update:bin, or update:application_controller)
rake routes                             # Print out all defined routes in match order, with names
rake secret                             # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions)
rake spec                               # Run all specs in spec directory (excluding plugin specs)
rake spec:models                        # Run the code examples in spec/models
rake stats                              # Report code statistics (KLOCs, etc) from the application
rake time:zones:all                     # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6
rake tmp:clear                          # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create                         # Creates tmp directories for sessions, cache, sockets, and pids

我感興趣的是為什么這個rake規范破壞​​了我的數據庫,因為當我使用相同的任務和'捆綁exec rspec'后,測試正在通過。

更新這是我的database.yml:

development:
 adapter: postgresql
 encoding: unicode
 database: pgdb
 pool: 15
 username: **************
 password: **************
production:
 adapter: postgresql
 encoding: unicode
 database: pgdb
 pool: 15
 username: **************
 password: **************
test:
  adapter: postgresql
  encoding: unicode
  database: pgdb
  pool: 15
  username: **************
  password: **************

提前致謝!

每當我使用bundle exec rspec spec手動運行單元測試時,我的數據庫都會被擦除一些類似的問題,結果是它正在使用開發環境,即使我在spec_helper.rb使用ENV["RAILS_ENV"] = 'test'手動設置它ENV["RAILS_ENV"] = 'test'

現在我只是明確指定RAILS_ENV=test bundle exec rspec spec或者你可以將它放在dotests.sh腳本中。

看到這些主題:

耙黃瓜和耙子規格總是使用“開發”環境

Rails 4,新應用程序:為什么測試在開發環境中運行? (我嘗試了Rails.env = 'test'東西,它對我不起作用,但Rails.env = 'test'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM