rails test を実行すると出てくるエラーの解決法がわかりません。
どこをどう直せばいいのでしょうか。どなたか教えていただけると幸いです。
やろうとしていること
アドミンではないユーザーがアドミン属性をwebからいじれないかどうかを確かめるテストをしようとしています。
テストコード自体はrailsチュートリアルの通りです。
ユーザーやユーザーの属性に関してテストをやろうとしているのに、何故photos.imageでのエラーが出るんだろう??というレベルです。。すみません。。
発生している問題・エラーメッセージ
Error: UsersControllerTest#test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web: ActiveRecord::NotNullViolation: SQLite3::ConstraintException: photos.image may not be NULL: INSERT INTO "photos" ("created_at", "updated_at", "id") VALUES ('2019-09-10 01:49:00.601772', '2019-09-10 01:49:00.601772', 980190962) bin/rails test test/controllers/users_controller_test.rb:10
該当のソースコード
↓ /insta-clone/test/controllers/users_controller_test.rb
require 'test_helper' class UsersControllerTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) @other_user = users(:archer) end test "should not allow the admin attribute to be edited via the web" do log_in_as(@other_user) assert_not @other_user.admin? patch user_path(@other_user), params: { user: { password: @other_user.password, password_confirmation: @other_user.password, admin: true } } assert_not @other_user.reload.admin? end end
フィクスチャ
/insta-clone/test/fixtures/users.yml
michael: name: Michael Example email: michael@example.com password_digest: <%= User.digest('password') %> admin: true archer: name: Sterling Archer email: duchess@example.gov password_digest: <%= User.digest('password') %>
nullと言われている photos.image のphotoモデルです。
/insta-clone/app/models/photo.rb
class Photo < ApplicationRecord validates :image, presence: true belongs_to :post mount_uploader :image, ImageUploader end
試したこと
・rails db:migrate:reset
補足情報
・rails チュートリアルの10章の10.56のテストです。
・チュートリアルの内容を所々参考にして抜き出して使用しているので、順当に最初からチュートリアルに従って作成した場合には出なかったエラーなのかもしれません。
情報の追加
/insta-clone/app/models/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, :omniauthable validates :name, presence: true, uniqueness: { case_sensitive: :false }, length: { maximum: 50 } has_many :posts, dependent: :destroy has_many :likes has_many :comments 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 def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.email = auth.info.email user.password = Devise.friendly_token[0,20] user.name = auth.info.name # assuming the user model has a name user.image = auth.info.image # assuming the user model has an image # If you are using confirmable and the provider(s) you use validate emails, # uncomment the line below to skip the confirmation emails. # user.skip_confirmation! end end def update_without_current_password(params, *options) params.delete(:current_password) if params[:password].blank? && params[:password_confirmation].blank? params.delete(:password) params.delete(:password_confirmation) end result = update_attributes(params, *options) clean_up_passwords result end def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) 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 end
あなたの回答
tips
プレビュー