Reputation: 19150
I'm using rails 5 with devise. I'm trying to write some unit tests for my controller, but am running into a wall. Here's the method I'm trying to test
# 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
and here's the test I'm writing
# 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
However, when I run it, I get the below exception ...
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
No where am I asking for a new record to be created, so why is my test trying to insert one?
Upvotes: 0
Views: 73
Reputation: 2258
In the line sign_in users(:one)
, users(:one)
is fetching a fixture. Seeing that tells me you're using a fixtures file.
There's a file users.yml
in your project which defines some users for testing. It looks like the ActiveRecord::RecordNotUnique
exception is coming from there. More specifically, a user fixture appears to be violating a uniqueness constraint on users.email
.
If you fix your users.yml
file by giving each user a unique email address, you should be good to go.
Upvotes: 1