Search code examples
ruby-on-railstestcase

Rails TestCase UndefinedColumn


New to the rails world. Using 4.2.4.

I'm trying to create a model for my app and write some unit tests for it, but I'm running into difficulty after running a change migration. I created a model with

rails generate model player first_name:string last_name:string dispaly_name:string

and ran rake db:migrate RAILS_ENV=test

Then I wrote a (failing) unit test to make sure the proper fields were set when calling save. At this point, I realized that I misspelled dispaly_name, so I created a change migration that fixed the column name.

Now when I try to run the unit test rake test test/models/player_test.rb, I get an error (UndefinedColumn) that says the save failed because the Player model is still trying to save with the dispaly_name instead of display_name. I ran rake db:migrate rake db:migrate RAILS_ENV=test rake db:test:prepare and ran rails c and RAILS_ENV=test rails c and checked that the column name was set properly by running Player.column_names. I also checked schema.rb. Everything seems to be in order, but I can't figure out why the Player.new in my test case is using the old column name.

Can someone explain what's happening?


Solution

  • I got it:

    when you create model then a fixture along with test file is created.

    you created your model with field name dispaly_name, and in file YourApp/test/fixtures/player_test.yml a field dispaly_name is created. right?.

    but the problem is that when you change field name to display_name it changes in MODEL and TABLE too but not in fixture file. so you need to correct your field in fixture file too.

    please change field name dispaly_name in file YourApp/test/fixtures/player_test.yml

    I hope it will work :)