我有一个简单的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
........
当我使用&#39;捆绑exec rake db:populate&#39;这个任务工作正常。我有完整的用户记录数据库。
我也有一些使用模型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规范破坏了我的数据库,因为当我使用相同的任务和&#39;捆绑exec rspec&#39;在测试结束后。
更新 这是我的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: **************
提前致谢!
答案 0 :(得分:2)
每当我使用bundle exec rspec spec
手动运行单元测试时,我的数据库被擦除时会遇到类似的问题,结果是它正在使用开发环境,即使我在{}中手动设置它{1}} spec_helper.rb
。
现在我只是明确指定ENV["RAILS_ENV"] = 'test'
,或者您可以将其放在RAILS_ENV=test bundle exec rspec spec
脚本中。
请参阅以下主题:
rake cucumber and rake spec always use "develop" environment
Rails 4, New App: Why do tests run in development environment?(我尝试了dotests.sh
这件事并没有对我有用,但是试一试)