Railsチュートリアルをしていたところテストコードが実行できなくなりました
エラーメッセージに乗っていたコマンドも実行しましたがそれもエラーが出てしまい動きませんでした
いりろ調べ試してみましたが全部無理でした
実行したコマンドとエラーメッセージ
Console
1> raiks test 2Migrations are pending. To resolve this issue, run: 3 4 rails db:migrate RAILS_ENV=test 5 6> rails db:migrate RAILS_ENV=test 7== 20210207212435 AddIndexToUsersEmail: migrating ============================= 8-- add_index(:users, :email, {:unique=>true}) 9rails aborted! 10StandardError: An error has occurred, this and all later migrations canceled: 11 12SQLite3::ConstraintException: UNIQUE constraint failed: users.email 13C:/Users/elinu/sample_app/db/migrate/20210207212435_add_index_to_users_email.rb:3:in `change' 14bin/rails:4:in `require' 15bin/rails:4:in `<main>' 16 17Caused by: 18ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email 19C:/Users/elinu/sample_app/db/migrate/20210207212435_add_index_to_users_email.rb:3:in `change' 20bin/rails:4:in `require' 21bin/rails:4:in `<main>' 22 23Caused by: 24SQLite3::ConstraintException: UNIQUE constraint failed: users.email 25C:/Users/elinu/sample_app/db/migrate/20210207212435_add_index_to_users_email.rb:3:in `change' 26bin/rails:4:in `require' 27bin/rails:4:in `<main>' 28Tasks: TOP => db:migrate 29(See full trace by running task with --trace)
ソースコード
Migrateコード
Ruby
1class DropTableUsers < ActiveRecord::Migration[6.0] 2 def change 3 end 4end
Ruby
1class CreateUsers < ActiveRecord::Migration[6.0] 2 def change 3 create_table :users do |t| 4 t.string :name 5 t.string :email 6 7 t.timestamps 8 end 9 end 10end
Ruby
1class AddIndexToUsersEmail < ActiveRecord::Migration[6.0] 2 def change 3 add_index :users, :email, unique: true 4 end 5end
Ruby
1class AddPasswordDigestToUsers < ActiveRecord::Migration[6.0] 2 def change 3 add_column :users, :password_digest, :string 4 end 5end
ソース
user.rb
Ruby
1class User < ApplicationRecord 2 before_save { self.email = email.downcase } 3 validates :name, presence: true, length: { maximum: 50 } 4 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 5 validates :email, presence: true, length: { maximum: 255 }, 6 format: { with: VALID_EMAIL_REGEX }, 7 uniqueness: true 8 has_secure_password 9 validates :password, presence: true, length: { minimum: 6 } 10end
テストコード
user_test.rb
Ruby
1require 'test_helper' 2 3class UserTest < ActiveSupport::TestCase 4 5 def setup 6 @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar") 7 end 8 9 test "should be valid" do 10 assert @user.valid? 11 end 12 13 test "name should be present" do 14 @user.name = "" 15 assert_not @user.valid? 16 end 17 18 test "email should be present" do 19 @user.email = " " 20 assert_not @user.valid? 21 end 22 23 test "name should not be too long" do 24 @user.name = "a" * 51 25 assert_not @user.valid? 26 end 27 28 test "email should not be too long" do 29 @user.email = "a" * 244 + "@example.com" 30 assert_not @user.valid? 31 end 32 33 test "email validation should accept valid addresses" do 34 valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org 35 first.last@foo.jp alice+bob@baz.cn] 36 valid_addresses.each do |valid_address| 37 @user.email = valid_address 38 assert @user.valid?, "#{valid_address.inspect} should be valid" 39 end 40 end 41 42 test "email validation should reject invalid addresses" do 43 invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. 44 foo@bar_baz.com foo@bar+baz.com] 45 invalid_addresses.each do |invalid_address| 46 @user.email = invalid_address 47 assert_not @user.valid?, "#{invalid_address.inspect} should be invalid" 48 end 49 end 50 51 test "email addresses should be unique" do 52 duplicate_user = @user.dup 53 @user.save 54 assert_not duplicate_user.valid? 55 end 56 57 test "password should be present (nonblank)" do 58 @user.password = @user.password_confirmation = " " * 6 59 assert_not @user.valid? 60 end 61 62 test "password should have a minimum length" do 63 @user.password = @user.password_confirmation = "a" * 5 64 assert_not @user.valid? 65 end 66end
users_signup_test.rb
Ruby
1require 'test_helper' 2 3class UsersSignupTest < ActionDispatch::IntegrationTest 4 test "invalid signup information" do 5 get signup_path 6 assert_no_difference 'User.count' do 7 post users_path, params: { user: { name: "", 8 email: "user@invalid", 9 password: "foo", 10 password_confirmation: "bar" } } 11 end 12 assert_template 'users/new' 13 assert_select 'div#error_explanation' 14 assert_select 'div.alert' 15 end 16 17 test "valid signup information" do 18 get signup_path 19 assert_difference 'User.count', 1 do 20 post users_path, params: { user: { name: "Example User", 21 email: "user@example.com", 22 password: "password", 23 password_confirmation: "password" } } 24 end 25 follow_redirect! 26 assert_template 'users/show' 27 assert_not flash.empty? 28 end 29end
データベースをリセットしたりしましたがうまく動きませんでした
バージョンなど
Ruby ruby 2.6.6p146 (2020-03-31 revision 67876) [x64-mingw32]
Ruby on Rails Rails 6.0.3
OS windows10
Gemfile
1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4gem 'rails', '6.0.3' 5gem 'bcrypt', '3.1.13' 6gem 'bootstrap-sass', '3.4.1' 7gem 'puma', '4.3.6' 8gem 'sass-rails', '5.1.0' 9gem 'webpacker', '4.0.7' 10gem 'turbolinks', '5.2.0' 11gem 'jbuilder', '2.9.1' 12gem 'bootsnap', '1.4.5', require: false 13 14group :development, :test do 15 gem 'sqlite3', '1.4.1' 16 gem 'byebug', '11.0.1', platforms: [:mri, :mingw, :x64_mingw] 17end 18 19group :development do 20 gem 'web-console', '4.0.1' 21 gem 'listen', '3.1.5' 22 gem 'spring', '2.1.0' 23 gem 'spring-watcher-listen', '2.0.1' 24end 25 26group :test do 27 gem 'capybara', '3.28.0' 28 gem 'selenium-webdriver', '3.142.4' 29 gem 'webdrivers', '4.1.2' 30 gem 'rails-controller-testing', '1.0.4' 31 gem 'minitest', '5.11.3' 32 gem 'minitest-reporters', '1.3.8' 33 gem 'guard', '2.16.2' 34 gem 'guard-minitest', '2.4.6' 35end 36 37group :production do 38 gem 'pg', '1.1.4' 39end 40 41# Windows ではタイムゾーン情報用の tzinfo-data gem を含める必要があります 42gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
どなたかよろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。