I have a simple unit test:
def test_conversation_without_a_name_is_invalid
conversation = Conversation.new
assert conversation.name.blank?
assert !conversation.valid?
end
that is failing with:
1) Error:
test_conversation_without_a_name_is_invalid(ConversationTest):
ActiveRecord::RecordNotUnique: PG::Error: ERROR: duplicate key value
violates unique constraint "index_admins_on_email" DETAIL: Key
(email)=() already exists.
It seems that for some reason the test is trying to create a Devise admin user and failing, but I have no idea why it is trying to do this in the first place, or even how to correct it.
I ran the same test in the rails console and it worked fine, very confused.
Has anyone run into this before or know what is going on? Any help would be appreciated!
Tracked it down to a fixtures:all statement in my test_helper.rb, still learning my way in Rails, but thanks to Alfonso for the great suggetions.
Related
So whenever I run a test, Rails seems to be attempting to insert nothing into my PostgreSQL database... causing a syntax error. What could possibly cause this? For some context, this happens with every test, regardless of how simple or complex. For example:
class PlayerTest < ActiveSupport::TestCase
test "should not save empty player" do
assert true
end
end
And then I see the following error message:
Error:
PlayerTest#test_should_not_save_empty_player:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near ")"
LINE 1: INSERT INTO "players_venues" () VALUES ()
Also, players_venues is a many-to-many join table between players and venues. This problem does not occur outside of tests. Any help would be greatly appreciated. If any more code is required please don't hesitate to ask.
So I eventually figured this out. It turns out Rails generates empty fixtures like so:
one: {}
# column: value
#
two: {}
# column: value
When you run the tests it attempts to insert nothing! Thanks heaps to jdno for giving my a hint to look into the fixtures. Bloody champion.
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.
I am finishing the book and I've ignored this error message for too long. Please help me understand how to fix this. Thank you!
1) Error:
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method reset_sent_at=' for #<User:0x007f814e118600>
app/models/user.rb:63:increate_reset_digest'
app/controllers/password_resets_controller.rb:12:in create'
test/integration/password_resets_test.rb:17:inblock in '
The error might point you a little bit into the wrong direction. This NoMethodError is actually caused by that fact that your users table doesn't have a column reset_sent_at.
Rails defines accessor methods (getters and setters) on your User model for each column in the users table. However, since it doesn't have a reset_sent_at column, no accessor is defined and the NoMethodError is being raised.
Make sure you've created the migration where reset_sent_at was added to users (this was done in chapter 10 of the Rails tutorial), and that you have executed the migration as well. After the migration, be sure to restart your Rails server.
When i check my database, matching_profiles.career_status_id clearly exists. So it's unclear to me why i'd be receiving this error message.
I've ran bundle exec rake db:migrate but that doesn't take care of it. I wouldn't expect it to since the column is in the database. I thought maybe there's something that is run as part of migrate to let the model know about the column or how to access the column.
In any case I'm not certain how to proceed from here. Any insight is greatly appreciated.
HI,
I got this error when i am testing my rails application. I dont have the table named 'instructions'. But it shows a error like "ERROR: relation "instructions" does not exist". Totally, I got same error for 64 tests as 64 errors.
I am using rails 3.0, Ruby 1.9.2, Netbeans 6.8.
PS: I didnt creat Instruction manual for rails application.
Error:
test_should_get_index(HomeControllerTest):
ActiveRecord::StatementInvalid: PGError: ERROR: relation "instructions" does not exist
LINE 1: DELETE FROM "instructions"
^
: DELETE FROM "instructions"
Kindly help me in this regard
You may want to run rake db:test:prepare , the table instructions isn't present in your test database.
Check your fixtures. I hit an error like this when using Rails scaffolding to generate a subclassed resource.
The generator had created files in my test/fixtures/ directory that didn't correspond to any actual tables in my database (since the model was using STI under another table).
When running tests, Rails attempted to instantiate all my fixtures, and an error similar to yours was generated when it could not find a table matching the name of the fixture.
Deleting the unneeded fixture file cleared my error and got tests running.
Hope that helps you, or others who arrive here searching that error, as I did. :]