2017-12-27 1294 views
0

我正在使用rails 5和devise。我正在爲我的控制器編寫一些單元測試,但我正在跑到一堵牆上。以下是我正在試圖測試爲什麼我的單元測試試圖插入一個記錄,當我不問它?

# GET /issues/new 
    def new 
    unless user_signed_in? 
     redirect_to new_user_session_path 
    end 
    @issue = Issue.new(stop_onestop_id: params[:stop_id], line_onestop_id: params[:line_id]) 
    end 

這裏的方法就是我寫

# issues_controller_test.rb 
class IssuesControllerTest < ActionDispatch::IntegrationTest 

    include Devise::Test::IntegrationHelpers 

    test "logged in should get issues page" do 
    sign_in users(:one) 
    test_stop_id = 1 
    test_line_id = 1 
    get new_issue_url, params: {stop_id: test_stop_id, line_id: test_line_id} 
    assert_equal test_stop_id, @issue.stop.id 
    assert_equal test_line_id, @issue.line.id 
    assert_response :success 
    end 
end 

然而測試,當我運行它,我得到以下例外...

localhost:Caravan-App davea$ rails test 
Running via Spring preloader in process 9509 
Run options: --seed 48437 

# Running: 

E 

Error: 
IssuesControllerTest#test_logged_in_should_get_issues_page: 
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2017-12-27 19:07:16.234345', '2017-12-27 19:07:16.234345', 298486374) 



bin/rails test test/controllers/issues_controller_test.rb:6 



Finished in 0.320746s, 3.1177 runs/s, 0.0000 assertions/s. 

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips 

沒有我要求創建新記錄的位置,爲什麼我的測試試圖插入一個?

+1

'用戶(:一個)'抓住測試夾具。你有沒有檢查你的'users.yml'夾具文件?你可能會有一些裝置違反了獨特的電子郵件限制。 –

+0

嘗試將'redirect_to new_user_session_path'更改爲'redirect_to new_user_session_path並返回' –

+0

@DerekHopper,使用taht fixture文件進行良好調用。這確實是問題。 – Dave

回答

1

在行sign_in users(:one),users(:one)正在取夾具。看到這告訴我你正在使用一個燈具文件。

您的項目中有一個文件users.yml,它定義了一些用戶進行測試。它看起來像ActiveRecord::RecordNotUnique異常來自那裏。更具體地說,用戶固定裝置似乎違反了對users.email的唯一性約束。

如果您通過爲每個用戶提供一個唯一的電子郵件地址來修復您的users.yml文件,那麼您應該很好。

相關問題