Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails test ActiveRecord error

I initiated the test environment in my rails app, and when I test the user model with the default code, it throws the following error:

Test code:

test "the truth" do
 assert true
end

1) Error:
UserTest#test_the_truth:
ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry '' for key 'index_users_on_email': INSERT INTO `users` (`created_at`, `updated_at`, `id`) VALUES ('2014-02-01 17:45:51', '2014-02-01 17:45:51', 298486374)

and inside my user model, I have the following associations

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
validates :user_name , :email, :first_name ,:last_name , :presence => true
has_many :invitations
has_many :incoming_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "friend_id", :through => :invitations
has_many :outgoing_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "user_id", :through => :invitations
like image 506
Coding active Avatar asked Jun 22 '26 20:06

Coding active


1 Answers

Firstly, check your user model fixture in test/fixtures/users.yml. If you have empty declarations of one and two:

one: {}
# column: value
#
two: {}
#  column: value

it can cause a problems, because there is a lack of attributes. Remove this part or comment it:

#one: {}
# column: value
#
#two: {}
#  column: value

And try run it again.

like image 196
nicq Avatar answered Jun 27 '26 04:06

nicq