2017-01-11 88 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" } 

希望它可以幫助..