Rails 3: Unit tests fail to run?
I am trying to set up my unit tests for my application but any time i try run any of my unit tests i get the follwoing error when i run the test in rubymine:
ActiveRecord::StatementInv开发者_运维技巧alid: Mysql2::Error: Table 'cms2_test.user_sessions' doesn't exist: DELETE FROM `user_sessions`
1 tests, 0 assertions, 0 failures, 1 errors
Test suite finished: 0.016993 seconds
and here is the error i get when i try run the test in the console:
ruby unit/user_test.rb
/Users/aldeirm2/Desktop/CMS2/config/boot.rb:1:in `require': no such file to load -- rubygems (LoadError)
from /Users/aldeirm2/Desktop/CMS2/config/boot.rb:1
from /Users/aldeirm2/Desktop/CMS2/config/application.rb:1:in `require'
from /Users/aldeirm2/Desktop/CMS2/config/application.rb:1
from /Users/aldeirm2/Desktop/CMS2/config/environment.rb:2:in `require'
from /Users/aldeirm2/Desktop/CMS2/config/environment.rb:2
from ./test_helper.rb:2:in `require'
from ./test_helper.rb:2
from unit/user_test.rb:1:in `require'
from unit
/user_test.rb:1
two diffrent errors!
ANy ideas who i can get my unit tests working?
Thanks
p.s I am using authlogic for authentcation not that i think it has anything to do with it.
The ActiveRecord::StatementInvalid
record seems like an outdated test database. Unless you are running a full rake
rather than running an individual spec, you need to run rake db:test:prepare
in order to update your database tables in your testing environment. That is basically just going to match the development database schema.
When running rake
you don't need to do so, it automatically does this for you before it starts running individual tests.
You may have trouble running unit tests directly unless you've set up your environment to support it as sometimes the Ruby interpreter has trouble resolving the location of test_helper
. The most reliable way of running them is rake test:units
and you can see the command-line constructed to execute that.
The first error may be related to having a fixture file without an associated table in the schema, which in this case implies you may have test/fixtures/user_sessions.yml
but no user_sessions
table defined in db/schema.rb
. Fixtures are loaded in for each test and the first step is to delete all the old records.
精彩评论