简体   繁体   中英

Ruby on Rails 4 fixtures require table

Fixtures when using Rails (My environment Rails 4.2)

Why when running a test, why does it error when a fixture exists but there isn't a table with the same name? And why does the fixture require to have a matching column in the table?

Example 1: (table does not exist)

  • create a fixture joes.yml (Joes table does not exist)
  • Run the rake test:integration
  • The error you'll get is:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: joes: DELETE FROM "joes"

Example 2: (table exists, but column does not)

  • create a fixture products.yml. This case the table Products does exist
  • add a yml block with a name of a column you don't have in the products table. example add an entry other: bla in the products.yml file
  • Run the rake test:integration

ActiveRecord::Fixture::FixtureError: table "products" has no column named "other".

I thought I could use fixtures as common place to reference in my tests, but doesn't need to match my DB.

Fixtures are your data, they still require a place to go!

"Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent written in YAML. There is one file per model."

http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures

They are not some sort of in memory replacement for a testing database, they just allow you to pre-populate your test database with values, so that you can test more conveniently without having to put lots of data in, when setting up a test run. That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM