前提・実現したいこと
rails testを通したい
https://qiita.com/johnslith/items/8d931b66f72c97c78199
上記の記事で実装を進めていったところrails testで同じ内容の大量エラー
users.unique_nameをどうすれば良いのかわかりません。
発生している問題・エラーメッセージ
test_should_get_new#SessionsControllerTest (6.81s) ActiveRecord::NotNullViolation: ActiveRecord::NotNullViolation: RuntimeError: NOT NULL constraint failed: users.unique_name 74/74: [===========] 100% Time: 00:00:06, Time: 00:00:06 Finished in 6.80925s 74 tests, 0 assertions, 0 failures, 74 errors, 0 skips 全部同じ内容のエラーになってしまう。
該当のソースコード
models/user.rb
class User < ApplicationRecord has_many :microposts, dependent: :destroy has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_save :downcase_unique_name before_create :create_activation_digest validates :name, presence: true, length: {maximum: 50} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i validates :email, presence: true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX}, uniqueness: true has_secure_password validates :password, presence: true, length: {minimum: 6},allow_nil: true VALID_UNIQUE_NAME_REGEX = /\A[a-z0-9_]+\z/i validates :unique_name, presence: true, length: { in: 5..15 }, format: { with: VALID_UNIQUE_NAME_REGEX }, uniqueness: { case_sensitive: false } class << self def digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end def new_token SecureRandom.urlsafe_base64 end end def remember self.remember_token = User.new_token update_attribute(:remember_digest, User.digest(remember_token)) end # トークンがダイジェストと一致したらtrueを返す def authenticated?(attribute, token) digest = send("#{attribute}_digest") return false if digest.nil? BCrypt::Password.new(digest).is_password?(token) end def forget update_attribute(:remember_digest, nil) end def activate update_columns(activated: true, activated_at: Time.zone.now) end def send_activation_email UserMailer.account_activation(self).deliver_now end def create_reset_digest self.reset_token = User.new_token update_columns(reset_digest: User.digest(reset_token), reset_sent_at: Time.zone.now) end def send_password_reset_email UserMailer.password_reset(self).deliver_now end def password_reset_expired? reset_sent_at < 2.hours.ago end def feed following_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id" Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id) end def follow(other_user) following << other_user end def unfollow(other_user) active_relationships.find_by(followed_id: other_user.id).destroy end def following?(other_user) following.include?(other_user) end private def downcase_email email.downcase! end def create_activation_digest self.activation_token = User.new_token self.activation_digest = User.digest(activation_token) end def downcase_unique_name self.unique_name.downcase! end end
models/user_test.rb
require 'test_helper' class UserTest < ActiveSupport::TestCase def setup @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar", unique_name: "example_user") end test "should be valid" do assert @user.valid? end test "name should be present" do @user.name = " " assert_not @user.valid? end test "email should be present" do @user.email = " " assert_not @user.valid? end test "email validation should accept valid addresses" do valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org first.last@foo.jp alice+bob@baz.cn] valid_addresses.each do |valid_address| @user.email = valid_address assert @user.valid?, "#{valid_address.inspect} should be valid" end end test "email validaion should reject invalid addresses" do invalid_addresses =%w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com foo@bar..com] invalid_addresses.each do |invalid_address| @user.email = invalid_address assert_not @user.valid?, "#{invalid_address.inspect} should be invalid" end end test "email addresses should be unique" do duplicate_user = @user.dup @user.save assert_not duplicate_user.valid? end test "email addresses should be saved as lower-case" do mixed_case_email = "Foo@ExAMPle.CoM" @user.email = mixed_case_email @user.save assert_equal mixed_case_email.downcase,@user.reload.email end test "password should be present (nonblank)" do @user.password = @user.password_confirmation = ""*6 assert_not @user.valid? end test "password should have a minimum length" do @user.password = @user.password_confirmation = "a"*5 assert_not @user.valid? end test "authenticated? should return false for a user with nil digest" do assert_not @user.authenticated?(:remember, '') end test "associated microposts should be destroyed" do @user.save @user.microposts.create!(content: "Lorem ipsum") assert_difference 'Micropost.count', -1 do @user.destroy end end test "should follow and unfollow a user" do michael = users(:michael) archer = users(:archer) assert_not michael.following?(archer) michael.follow(archer) assert michael.following?(archer) assert archer.followers.include?(michael) michael.unfollow(archer) assert_not michael.following?(archer) end test "feed should have the right posts" do michael = users(:michael) archer = users(:archer) lana = users(:lana) lana.microposts.each do |post_following| assert michael.feed.include?(post_following) end michael.microposts.each do |post_self| assert michael.feed.include?(post_self) end archer.microposts.each do |post_unfollowed| assert_not michael.feed.include?(post_unfollowed) end end end
users_login_test.rb
省略 test "valid signup information" do get signup_path assert_difference 'User.count', 1 do post users_path, params: { user: { name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password", unique_name: "example_user" } } end follow_redirect! end
users_signup_test.rb
require 'test_helper' class UsersSignupTest < ActionDispatch::IntegrationTest def setup ActionMailer::Base.deliveries.clear end test "invalid signup information" do get signup_path assert_no_difference 'User.count' do post users_path, params: { user: {name: "", email: "user@invalid", password: "foo", password_confirmation: "bar", unique_name: "" }} end assert_template 'users/new' assert_select 'div#error_explanation' assert_select 'div.field_with_errors' assert_select 'form[action="/signup"]' end test "valid signup information with account act ivation" do get signup_path assert_difference 'User.count', 1 do post users_path, params: {user: {name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password", unique_name: "example_user" }} end assert_equal 1, ActionMailer::Base.deliveries.size user = assigns(:user) assert_not user.activated? log_in_as(user) assert_not is_logged_in? get edit_account_activation_path("invalid token", email: user.email) assert_not is_logged_in? get edit_account_activation_path(user.activation_token, email: 'wrong') assert_not is_logged_in? get edit_account_activation_path(user.activation_token, email: user.email) assert user.reload.activated? follow_redirect! assert_template 'users/show' assert is_logged_in? end end
試したこと
typoがないか一度確認はしました。
rails db:migrate:reset
rails db:seed
の再実行
エラーについて少し調べてみましたがわかりませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/27 11:02
2021/08/27 11:32
2021/08/27 11:59 編集
2021/08/27 12:28
2021/08/27 12:33
2021/08/27 12:38
2021/08/28 08:53
2021/08/28 08:55
2021/08/28 10:01
2021/08/29 19:57
2021/08/29 22:39
2021/08/30 04:56
2021/08/30 06:08
2021/08/30 20:10
2021/08/31 02:49
2021/08/31 20:01
2021/08/31 22:28
2021/09/01 20:11
2021/09/01 20:18
2021/09/01 22:50
2021/09/03 08:10
2021/09/03 10:18
2021/09/04 09:43
2021/09/04 11:00
2021/09/04 23:20