How to use ruby-on-rails fixtures for setting up (external) data that does not belong to the rails app DB?

巧了我就是萌 提交于 2019-12-24 02:52:57

问题


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.

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

  • Added sample test data to a fixture file name 'external_database.yml'
  • Put the below code in setup, in the test file

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

  • But I am stuck with the below error

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. What is the best way to do what I want done?

回答1:


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.

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.



来源:https://stackoverflow.com/questions/5830541/how-to-use-ruby-on-rails-fixtures-for-setting-up-external-data-that-does-not-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!