捆绑执行佣金规格和自定义佣金任务 [英] Bundle exec rake spec and custom rake tasks

查看:80
本文介绍了捆绑执行佣金规格和自定义佣金任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的rake任务,专用于填充我的postgresql数据库(用于开发,测试和生产环境的同一数据库):

I have a simple rake task, dedicated for populating my postgresql database (same db for dev, test and prod environments):

# 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时,此任务运行正常。我有完整的用户记录数据库。

When I use 'bundle exec rake db:populate' this task is working fine. I have full db of User records.

我也有一些rspec测试,这些测试使用的是模型User,并且命令出现问题

I also have some rspec tests, which are using model User, and a problem with command

bundle exec rake spec

此命令由于某种原因破坏了我的数据库,我得到了以下测试结果:

This command for some reason ruins my db and I have the following result of tests:

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

After this rake task I can see that User.count returns 0

Rakefile,这是我所有耙任务的列表:

I have default Rakefile and here is the list of all my rake tasks:

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 spec破坏了我的数据库,因为当我通过相同的任务并且在测试通过后使用 bundle exec rspec时。

I am interested why this rake spec ruins my db, because when I am using the same task and 'bundle exec rspec' after that tests are passing.

更新
这是我的数据库。yml:

UPDATE Here is my 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: **************

谢谢!

推荐答案

每当我使用手动运行单元测试时,我的数据库就被抹掉了一些类似的问题捆绑了exec rspec spec ,结果证明它正在使用开发环境,即使我在 spec_helper.rb 中手动设置了它也是如此 ENV [ RAILS_ENV] ='test'

I had some similar issues with my DB getting wiped whenever I would run the unit tests manually with bundle exec rspec spec, and it turned out to be that it was using the development environment even though I was setting it manually in spec_helper.rb with ENV["RAILS_ENV"] = 'test'.

现在,我只是明确指定 RAILS_ENV = test bundle exec rspec spec 或您可以将其放在 dotests.sh 脚本中。

Now I just explicitly specify RAILS_ENV=test bundle exec rspec spec or you can put that in a dotests.sh script.

请参阅以下线程:

rake cucumber and rake spec always use "develop" environment

Rails 4,新应用:为什么测试要在开发环境中运行?(我尝试过 Rails.env ='test'东西,但它对我不起作用,但请尝试一下)

Rails 4, New App: Why do tests run in development environment? (I tried the Rails.env = 'test' thing and it didn't work for me, but give it a shot)

这篇关于捆绑执行佣金规格和自定义佣金任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆