2

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?
Sumeet Pareek
  • 2,739
  • 2
  • 22
  • 17

1 Answers1

2

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.

flitzwald
  • 20,200
  • 2
  • 32
  • 28
  • Thanks for the suggestion. I think it is taking me in the right direction. But I am not quite sure how to have my test ENV use the data. Also, there are many different methods in my ext_db model that pull variety of data (and I will have to add more in future), so it would be easier for me if I could create more test data easily. That is the reason I was thinking on the lines of loading a YAML file in a hash. – Sumeet Pareek Apr 29 '11 at 10:31