简体   繁体   English

如何在Rails测试中使用现有数据库?

[英]How do I use an Existing Database in my Rails Tests?

I have a postgreSQL database with sample users, content and other data. 我有一个包含示例用户,内容和其他数据的postgreSQL数据库。 How can I use it as my test database? 如何将其用作测试数据库?

The tests will create new data in it, so I want to be able to easily reset it before each run of tests. 测试将在其中创建新数据,因此我希望能够在每次运行测试之前轻松重置它们。 Is there an easy way to convert a database into a format that can be used to easily re-create it? 有没有一种简单的方法可以将数据库转换为可以轻松重新创建的格式?

This is the bad Idea to do but you can do this by changing config/database.yml as 这是个不好的主意,但是您可以通过将config / database.yml更改为

development:
  adapter: postgresql
  encoding: unicode
  database: development_db 
  pool: 10
  username: username
  password: password
  host: localhost

test 
  adapter: postgresql
  encoding: unicode
  database: test_db #change this db name to your development_db or whatever you want to use is test environment 
  pool: 10
  username: username
  password: password
  host: localhost

Change Test environment database to which you want to use(database with sample users) 更改要使用的测试环境数据库(带有示例用户的数据库)

Note: If you want re-use original database again then I would suggest take backup of database with sample users then use this in test environment 注意:如果要再次使用原始数据库,则建议与示例用户一起备份数据库,然后在测试环境中使用

If your data is not too complex, I would use fixtures for the job. 如果您的数据不太复杂,那么我会使用固定装置。 The test database is rolled back after every test which can slow down your tests considerably when your test suite grows and your database must be prepopulated before every test. 每次测试后都会回退测试数据库,这会在测试套件增长时大大降低测试速度,并且必须在每次测试之前预先填充数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何将RSpec与Rake,Rails和我现有的测试集成? - How should I integrate RSpec with Rake, Rails and my existing tests? 我在哪里将我的数据库查询测试放在 Rails 中? - Where do I put my database query tests in rails? 如何使用现有的SQL数据库将Rails应用程序部署到Heroku? - How do I deploy my rails app to Heroku using an existing SQL database? Ruby on Rails 2.3.8:测试:如何设置实例变量以在整个测试中使用? - Ruby on Rails 2.3.8: Testing: How do I set up an instance variable to use throughout my tests? 如何使用Twitter中的Bootstrap样式表和Rails中的现有样式表而不会搞乱一切? - How do I use the Bootstrap stylesheet from Twitter with my existing stylesheet in Rails without messing up everything? 如何在Rails中查询导入的数据库中的现有记录? - How can I query the existing records in my imported database in Rails? 如何在集成测试中启动rails服务器? - How do I start the rails server as part of my integration tests? rails-如何在测试中包括视图助手? - rails - how do i include view helpers in my tests? 如何在 Rails 6 中使用第二个数据库 - How do I use the second database in Rails 6 如何让Rails使用内存缓存而不是进入数据库? - How do I get Rails to use my memory cache instead of oging to the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM