依赖于另一个应用程序的外部数据库的应用程序的 RSpec 设置
我必须向依赖于另一个应用程序的数据库的应用程序添加功能。我已经能够建立与此外部数据库的连接并从中提取数据。但是,我不确定如何让我的主应用程序为此外部应用程序创建测试数据库。
如果有某种方法可以提取该数据库的模式并以与“rake db:test:prepare”相同的方式创建它,那就太棒了。 RSpec 是否有任何配置功能可以执行此操作,或者我必须滚动自己的任务?
I've had to add features to an application that depends on a database from another application. I've been able to set up a connection to this external database and pull data from it. However, I'm not sure how to get my main application to create a test database for this external application.
It would be awesome if there some way to pull in the schema for this database and create it in the same manner that 'rake db:test:prepare' does. Is there any configuration capabilities for RSpec to do this, or will I have to roll my own task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道有什么简单的方法可以做到这一点。
您可以准备一个 sql 模式,并通过自定义 rake 任务将其加载到您的测试数据库中,
但如果它是用于测试,那么您研究一下工厂可能是个好主意
例如 Factory Girl
“factory_girl 是一个固定装置替代品,具有简单的定义语法,支持多种构建策略(已保存的实例、未保存的实例、属性哈希和存根对象),并支持同一类的多个工厂(user、admin_user 等),包括工厂继承。”
以及截屏
I am not aware of any simple way to do this.
You could have a sql schema prepared and load it in your test database trough a custom rake task
But if it is for testing, it might be a good idea for you to look into Factories
For example Factory girl
"factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance."
And a screencast
这是 Rails 应用程序吗?如果是这样,即使您使用的是 RSpec,db:test:prepare 任务和相关任务仍然可以工作。
Is this a Rails app? If so, the db:test:prepare task and the related tasks will still work, even if you are using RSpec.
我最终只是为外部应用程序运行 rake db:test:prepare 并更新我正在开发的应用程序的数据库配置以包含外部应用程序的测试数据库。我想为外部应用程序自动化
rake db:test:prepare
,但是我不认为自己经常更新其架构。I ended up just running
rake db:test:prepare
for the external application and update the database configs of the application I'm developing to include the test DB for the external application. I'd like to automate therake db:test:prepare
for the external app, however I don't see myself updating its schema that often.