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
没有我要求创建新记录的位置,为什么我的测试试图插入一个?
'用户(:一个)'抓住测试夹具。你有没有检查你的'users.yml'夹具文件?你可能会有一些装置违反了独特的电子邮件限制。 –
尝试将'redirect_to new_user_session_path'更改为'redirect_to new_user_session_path并返回' –
@DerekHopper,使用taht fixture文件进行良好调用。这确实是问题。 – Dave