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.
Related
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.
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.
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.
I'm having a weird problem with my Rspec test suite. All tests that insert data into a table that has a unique constraint fail. Running the failing tests individually by specifying the line number works as expected.
To investigate the issue I printed the number of rows in that table before inserting the data that is causing the constraint exception and it reports that the table is empty.
There is no before :all in any file at all.
I'm using DatabaseCleaner and, except for this issue, it seems that the cleaning is working.
This e.g. doesn't work when running the whole file:
describe User
# ...
describe '#follow_location' do
let(:user) { Factory(:user) }
let(:location) { Factory(:location) }
it 'should raise when trying to follow an already followed location' do
puts LocationFollowship.count # => 0
user.followed_locations << location # exception raised
lambda {
user.follow_location location
}.should raise_error User::AlreadyFollowingLocation
end
end
# …
end
EDIT:
I was able to track it down. It has to do with Rails using a cached statement. Calling ActiveRecord::Base.connection.clear_cache! makes it work. But adding this snippet in spec_helper.rb causes an SQLite exception cannot use a closed statement.
I had this problem too. In addition, the Ruby interpreter would segfault under certain conditions when I tried to investigate it (probably caused by SQLite).
I have a unique index declared, like so:
add_index(:table, [:column1, :column2], unique: true)
Adding the following uniqueness constraint to the model (in addition to the existing index in the migration) made the issue go away:
validates_uniqueness_of :column1, scope: :column2
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. :]