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.
Related
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.
I am following through a simple tutorial and running into the following issue;
Task.create task: 'This is my task'
Returns an error when rspec tries to run it;
ActiveRecord::StatementInvalid:
Could not find table 'tasks'
But when I call the exact same line from the rails console or from a controller the task is created and I am able to see the new row from within the rails console.
Initially I thought it was maybe something going weird with guard, because I have noticed a few odd things (Ctrl+C doesn't kill it for one) but I decided to run the test directly using rspec and it returns the same result.
Any help would be greatly appreciated.
You have to set up and prepare your db first and you can do that by running rake db:test:prepare
My Ruby version is ruby 1.9.3p385 . My Rails version is 3.2.12.
I use the following to load the test database:
rake db:seed RAILS_ENV=test
Everything works great. All the Factory Girl stuff is loaded nicely.
Contents of test/factories.rb contains the factories. The contents of db/seeds.rb is simply:
FactoryGirl.create(:music_categorization)
FactoryGirl.create(:dance_categorization)
Now, when I run the tests using the following command:
rake test
Rails deletes all the seed data and the tests fail. I'd like a way to prevent "rake test" from deleting data.
The other route I went was to load the seed data as part of the "rake test" command as mentioned in this question. However, what that ends up doing is loading the test data twice (basically db/seeds.rb is called twice for some reason). I abandoned that route and now simply want to do the following to run my tests:
rake db:seed RAILS_ENV=test
rake test
Any help is appreciated. I basically want to prevent rake test from deleting my data OR figure out a way to not have db/seeds.rb called twice.
AFAIK its not usual to use db:seed to load data for your tests, its normally just used to load seed data for development purposes only.
Instead you should create test data in the actual test file. Usually test data is deleted after each test using something like database_cleaner, so each test starts with an empty database.
For example in rspec you can load test data in a let block, before block or in the test itself, e.g
require 'spec_helper'
describe Page do
let(:user) { FactoryGirl.create(:user) }
before do
# create some data
end
it '#name returns XYZ' do
page = FactoryGirl.create(:page, :user => user)
page.description.should == 'XYZ'
end
it '#description returns ABC' do
page = FactoryGirl.create(:page, :user => user)
page.description.should == 'ABC'
end
end
First, let me just say that in general you should not be using seed data in your tests. Tests are supposed to run with an empty database, and you create only the data you need for each test.
In your case, you seem to have not understood the meaning of the seed data, which is basically core data that your application needs to work properly. If you need to instantiate a couple of models in your tests, simply do (assuming you're using rspec)
before(:each) do
FactoryGirl.create(:music_categorization)
FactoryGirl.create(:dance_categorization)
end
If you still want to run the tests with the seed data, you can always try and run rspec, which will just run all you rspec examples leaving the test database as is. But believe, that's not what you want.
I try to run this simplest unit test:
test "the truth" do
assert true
end
like this:
ruby -Itest test/unit/my_model_test.rb
(there is the only one test in the file, and actually in the whole site)
it fails with this message:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column email is not unique: INSERT INTO "users"... [some values to insert here]
now I do have a table named "users" but other than that I have never asked to do any tests on it nor do I even have such tests. Where does this error come from? How do I run just my test?
EDIT: the "users" table comes from the devise gem if that helps
Quote from http://guides.rubyonrails.org/testing.html
Rails by default automatically loads all fixtures from the
test/fixtures folder for your unit and functional test. Loading
involves three steps:
Remove any existing data from the table corresponding to the fixture
Load the fixture data into the table
Dump the fixture data into a
variable in case you want to access it directly
So I think you need to check those fixtures. Also I suggest you to read that tutorial. It is very well written.
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.