[英]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.