2017-01-11 89 views
0

我对在Rails中测试非常新。我正在尝试为我的相册控制器中的索引操作创建一个非常基本的测试。我收到一个错误,不是在这个测试中,而是在我所有的测试中。错误看起来是这样的:Rails测试错误

bin/rails test test/controllers/albums_controller_test.rb:18 

E 

Error: 
AlbumsControllerTest#test_should_get_edit: 
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraintfailed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2017-01-11 21:54:05.906006', '2017-01-11 21:54:05.906006', 298486374) 

我所有的测试中得到这个错误,不只是这一个。这是我试图用上面的例子中运行测试:

require 'test_helper' 

class AlbumTest < ActiveSupport::TestCase 
test "index action should work" do 
get :index 
assert_response :success 
end 
end 

这里是我的专辑控制器中的索引操作:

def index 
    @albums = Album.all.order(year: :desc).order(title: :asc) 
end 

不知道发生了什么事情。帮助将不胜感激!

+0

你可以用'albums_controller_test.rb'编辑代码 –

回答

0

我需要更多的代码才能提供更好的解决方案,但是由于错误,您似乎尝试使用相同的电子邮件创建多个用户。而且由于您正在验证它们是唯一的,所以测试失败。

所以我认为这个问题必须在其他地方,你为测试创建用户。

如果你使用的灯具,你可以尝试这样的事:

john: 
    name: $LABEL 
    email: [email protected] 

如果您使用的是工厂的女孩,你可以尝试这样的事:

factory :user do 
    name    "John" 
    sequence(:email) { |n| "email#{n}@example.com" } 

希望它可以帮助..