如何使用ruby-on-rails固定装置设置不属于rails应用程序数据库的(外部)数据? [英] How to use ruby-on-rails fixtures for setting up (external) data that does not belong to the rails app DB?

查看:73
本文介绍了如何使用ruby-on-rails固定装置设置不属于rails应用程序数据库的(外部)数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的要求,我已经创建了一些模型,用于查询某些数据的外部数据库(与Rails应用程序使用的模型不同).

Per my requirements, I have created models for querying external database (different from the one that the rails app uses) for some of the data.

我正在尝试围绕这些模型编写测试,并希望将样本测试数据"与实际测试分开.

I am trying to write tests around these models and want to separate the "sample test data" from the actual tests.

我以为我可以将数据放在yml文件中,然后将其加载到哈希中,但是确实可以解决:(

I thought I could put the data in a yml file and load it into a hash, but it did work out :(

  • 将样本测试数据添加到夹具文件名为"external_database.yml"的
  • 在安装文件中的测试文件中放入以下代码

ext_data = YAML.load_file(Rails.root.to_s + "/test/fixtures/ext_data.yml")

ext_data = YAML.load_file(Rails.root.to_s + "/test/fixtures/ext_data.yml")

  • 但是我遇到了以下错误

1) Error: test_should_errorout_for_invalid_market_zip(ExtDBTest): ActiveRecord::StatementInvalid: Mysql::Error: Table 'rails_app_db.ext_data' doesn't exist: DELETE FROM ext_data

1) Error: test_should_errorout_for_invalid_market_zip(ExtDBTest): ActiveRecord::StatementInvalid: Mysql::Error: Table 'rails_app_db.ext_data' doesn't exist: DELETE FROM ext_data

  1. 做我想做的最好的方法是什么?

推荐答案

我猜您的问题是,您的schema.rb文件和/或迁移中不包含该外部数据库的架构.这些用于在运行测试之前设置测试数据库.

I guess your problem is that the schema of that external database is not contained in your schema.rb-file and/or migrations. These are used to setup your test-database before you run the tests.

因此,我们尝试将这些装置写入不存在的表中-您在上面看到的结果.

So the attempt is made to write those fixtures into non-existing tables - with the result you see above.

单元测试中的多个数据库连接通常很痛苦.考虑为外部依赖项的数据创建一个sqlite文件,并配置测试环境以使用此文件或它的副本,以防万一需要数据突变.

Multiple database-connections in unit tests are generally a pain. Consider creating an sqlite-file for the data of the external dependencies and configure your test-environment to use this file - or a copy of it, in case you need to mutate data.

这篇关于如何使用ruby-on-rails固定装置设置不属于rails应用程序数据库的(外部)数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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