I'm building an app in Rails 3 using Authlogic for authentication. I have a User model with a database table and a user_session model without one
All of my tests fail, whether I run
Error:
test_the_truth(UsersControllerTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table:
user_sessions: DELETE FROM "user_sessions" WHERE 1=1
It's expecting user_session to have a table even though it inherits from Authlogic. Does anybody know how to fix this?
I was having the same problem and this took me a while to discover... the thing is Authlogic has no table in the database. When we create the sessions with Rails generate, this is creating also automatically a fixture, which of course will fail later since there is no table to fill data in. Solution: delete the fixture of user_sessions.
Read more about the problem here
Ensure you have defined properly test database in config/database.yml, then try rake db:test:prepare or rake db:migrate RAILS_ENV=test.
Related
I created a model with a column named errors and when I ran the test I naturally got an ActiveRecord::DangerousAttributeError
so I rolled back the migration and changed the column name to error_messages and reran the migration. I don't refer to that column yet in any other part of my code.
Now I can create valid objects from this model in the rails console, but still the test is giving the same error. How do I make this error go away?
I needed to refresh the database structure in the testing environment like this:
bundle exec rake db:reset RAILS_ENV=test
I had done a lynda.com tutorial that created a simlple cms using rails. I wanted to practice and expand my knowledge of rails a bit more by adding the unit testing. But every time I try to run any test I get a mysql error. One example of this is just trying to get the index on the public controller.
#public_controller_test.br
test "should get index" do
get public_index_url
assert_response :success
end
It gives me the following error
PublicControllerTest#test_should_get_index:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'simple_cms_test.users' doesn't exist: DELETE FROM `users`
If you need to look at more of the code it is available at
https://github.com/trmccormick/rails_simple_cms Thanks for any assistance.
Error says you don't have table users in your simple_cms_test database.
So you need to sync your test db with dev db.
Run rake db:migrate test
or rake db:schema:load test and everything should work fine.
I figured this problem out on my own. The issue was in the fixtures I had a file users.yml it should have been admin_users.yml so the users table was never getting loaded with fake users so it was failing. Renaming the file corrected the issue.
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?
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 :)
Depending upon what you are using to populate your test db, FactoryGirl, Fabrication or the default Fixtures a Rails application come up with, change the data field name there. Then the error will disappear.
Do try restarting the server just in case if the error persists. I dont think so restarting the server will have any effect but just for a try.
Try to run bundle exec rake:test:clone, this recreates test db schema from current development one.
I've added a dependency to both order and order_items fixtures (which already existed), but I'm receiving the following error every time I run my rspec worker test.
ActiveRecord::StatementInvalid:
Mysql2::Error: Table 'inventory_test10.order_packages' doesn't exist: SHOW FULL FIELDS FROM `order_packages` /*controller:,action:,line:*/
I have an order which has many order_items and many order_packages. order_items also belong to order_packages. Therefore, I am able to do:
order.order_items.each do |oi|
put oi.order_package.status
end
The original issue was that status wasn't recognized for nil class because an order_packages.yml fixture was never created. I've tried several rake tasks, but I'm not super familiar with fixtures, migrations, rake tasks, etc and I'm not sure if I accidentally caused the error running multiple taks. Below is a snippet from a blog that warned about running the command multiple times - http://brandonhilkert.com/blog/using-rails-fixtures-to-seed-a-database/:
rake db:fixtures:load FIXTURES=credit_card_types
A word of warning, if we run this command multiple times, it will seed
the table multiple times. It’s not idempotent.
Other tasks I ran:
FIXTURES=orders; rake db:fixtures:load
rake db:fixtures:dump (didn't work - error)
rake db:fixtures:drop (didn't work - error)
Thanks in advance for any suggestions!
Your test framework should automatically load fixtures at the beginning of the test run, and delete them at the end of the test run. You should not need to load fixtures yourself.
Fixtures load data into tables; they do not alter the database structure. Migrations can alter the database by creating/dropping tables, adding/removing columns, etc. If you are having an issue with a missing table, it is very like a migration problem.
I recommend a review of the Guide to Testing Rails Applications, and (if you are using RSpec) the rspec-rails documentation, which explain these concepts in greater depth.
This may be my most stupid question yet:
I've implemented this tutorial on my Rails 3 app to get Devise up and running with CanCan. What is the right way to enter the first record in your development database, when only admins are allowed to enter a record and a null record isn't allowed? Do you just hack away some of the code to reduce permissions, then put it back again?
Update: to add to the answer, if you put this in seeds.db it works, even though there's no password field in the User table:
User.create(:email => 'me#mysite.com',:password => 'secret')
Use db/seeds.rb to write the code for your admin record. And call rake db:seed to execute that. Make sure you check whether the initial records are already created, so that running rake db:seed won't duplicate the data.