ruby-on-rails-4 – Rails测试ActiveRecord错误
发布时间:2020-12-17 01:31:58 所属栏目:百科 来源:网络整理
导读:我在rails应用程序中启动了测试环境,当我使用默认代码测试用户模型时,它会抛出以下错误: 测试代码: test "the truth" do assert trueend1) Error:UserTest#test_the_truth:ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry '' for key 'ind
我在rails应用程序中启动了测试环境,当我使用默认代码测试用户模型时,它会抛出以下错误:
测试代码: 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) 在我的用户模型中,我有以下关联 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,:foreign_key => "user_id",:through => :invitations 解决方法
首先,检查test / fixtures / users.yml中的用户模型夹具.如果您有一个和两个的空声明:
one: {} # column: value # two: {} # column: value 它可能会导致问题,因为缺乏属性.删除此部分或评论它: #one: {} # column: value # #two: {} # column: value 然后尝试再次运行它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |