I'm am trying to execute the default functional tests in my Rails 3.2.3 project. When I do, I get something like the following error multiple times:
|| test_should_create_food_item(FoodItemsControllerTest): || ActiveRecord::InvalidForeignKey: PG::Error: ERROR: insert or update on table "food_items" violates foreign key constraint "food_items_us
My food_item records have a foreign key that points to a user id in the user table.
It's my understanding that Rails creates and destroys the test data in the "test" database with every test method invocation. If this is true, then this error makes sense because the users table would be empty while the record is being inserted into the food_items table.
Here's what I've done to try and fix this problem. First, I made sure that the relevant models have the appropriate has_many and belongs_to sections.
Next, I added the following to my setup function in food_items_controller_test.rb:
setup do > @user = users(:one) @food_item = food_items(:one) end
I also tried adding the @user invocation to the individual tests, but it didn't make any difference.
Is there something else I can do to create the user record before I execute the functional tests in food_items_controller_test?
Also, if it's possible, I would rather not turn off the DB constraints in the "test" database.