前提・実現したいこと
railsで作成したアプリケーションにtestを実装していますが下記エラーが出てtestが実行できません。
登録したアカウントでE-mailが同じものがあるのでエラーが出たのかと思い、rails db:resetを実行してから試したのですが同じ結果でした。
どのように改善すべきでしょうか。
発生している問題・エラーメッセージ
Error: ArticlesControllerTest#test_should_get_show: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column email is not unique: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2019-12-24 08:33:36.597587', '2019-12-24 08:33:36.597587', 298486374)
articles_controller_test.rb
require 'test_helper' class ArticlesControllerTest < ActionDispatch::IntegrationTest test "should get index" do get articles_index_url assert_response :success end test "should get show" do get articles_show_url assert_response :success end test "should get edit" do get articles_edit_url assert_response :success end test "should get new" do get articles_new_url assert_response :success end end
routes.rb
Rails.application.routes.draw do devise_for :users, :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions' } resources :articles root "articles#index" # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_one :profile, dependent: :destroy has_many :articles, dependent: :destroy delegate :name, to: :profile end
articles.yml
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: title: MyString body: MyText two: title: MyString body: MyText
database.yml
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3
test.sqlite3
SQLite format 3@ -�)��� ���<���k�)tablearticlesarticlesCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "body" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "user_id" integer, "image_id" varchar)P++Ytablesqlite_sequencesqlite_sequenceCREATE TABLE sqlite_sequence(name,seq)p?�indexindex_articles_on_user_idarticlesCREATE INDEX "index_articles_on_user_id" ON "articles" ("user_id")�]� tableprofilesprofilesCREATE TABLE "profiles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "name" varchar DEFAULT '' NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)p?�indexindex_profiles_on_user_idprofilesCREATE INDEX "index_profiles_on_user_id" ON "profiles" ("user_id") ��k�W� tableusersusersCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar DEFAULT '' NOT NULL, "encrypted_password" varchar DEFAULT '' NOT NULL, "reset_password_token" varchar, "reset_password_sent_at" datetime, "remember_created_at" datetime, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)e5�indexindex_users_on_emailusers CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")�S�Gindexindex_users_on_reset_password_tokenusersCREATE UNIQUE INDEX "index_users_on_reset_password_token" ON "users" ("reset_password_token") �������)20191204031155)20191202103026)20191204030243)20191204152522)20191204022005)20191205081943 �������)20191204031155)20191202103026)20191204030243)20191204152522)20191204022005) 20191205081943 ��QH#AAenvironmenttest2019-12-25 12:08:45.7202332019-12-25 12:08:45.727612 ��# environment ))���x //�tableschema_migrationsschema_migrationsCREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY)A U/indexsqlite_autoindex_schema_migrations_1schema_migrations �N55�?tablear_internal_metadataar_internal_metadataCREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)G[5indexsqlite_autoindex_ar_internal_metadata_1ar_internal_metadata
schema.rb
ActiveRecord::Schema.define(version: 20191205081943) do create_table "articles", force: :cascade do |t| t.string "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" t.string "image_id" t.index ["user_id"], name: "index_articles_on_user_id" end create_table "profiles", force: :cascade do |t| t.integer "user_id" t.string "name", default: "", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["user_id"], name: "index_profiles_on_user_id" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
test_helper.rb
ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' class ActiveSupport::TestCase Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all Add more helper methods to be used by all tests here... end
users.yml
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the # model remove the '{}' from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # one: {} # column: value # two: {} # column: value
補足情報(FW/ツールのバージョンなど)
AWS Cloud9
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/25 12:17
2019/12/25 12:53
2019/12/26 04:32
2019/12/26 05:09
2019/12/26 06:13
2019/12/26 07:50 編集
2019/12/29 06:04 編集
2019/12/29 08:09
2019/12/30 12:27
2019/12/30 14:46
2019/12/31 04:14
2019/12/31 04:18
2019/12/31 10:04
2019/12/31 10:38
2019/12/31 12:27
2019/12/31 12:34
2020/01/05 04:28
2020/01/05 06:28
2020/01/05 09:01