質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

1回答

418閲覧

modelspecが通らない

ik_ko

総合スコア9

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2021/03/26 13:09

ポートフォリオ作成中の初学者なのですがエラーが発生し躓いている状況なのでわかる方いましたらご教授お願いします。
#状況
articleモデルのテストを以下のように記述しテストを通そうとしましたがエラーが発生しました。
ですがどうコードを記述しなおせば良いか検索してもわからない状況です。
category_idを入れているのにもかかわらずカテゴリが入力されていないと認識されています。

#エラーログ

sdf-app % bundle exec rspec spec/models/article_spec.rb warning: parser/current is loading parser/ruby26, which recognizes warning: 2.6.6-compliant syntax, but you are running 2.6.5. warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (2 levels) in <top (required)> at /Users/imayoshikosuke/sdf-app/spec/models/article_spec.rb:23) /Users/imayoshikosuke/sdf-app/spec/factories/articles.rb:4: Passing `digits` with the 1st argument of `number` is deprecated. Use keyword argument like `number(digits: ...)` instead. To automatically update from positional arguments to keyword arguments, install rubocop-faker and run: rubocop \ --require rubocop-faker \ --only Faker/DeprecatedArguments \ --auto-correct F Failures: 1) Article 全ての項目が入力されている場合 記事一覧を保存できる Failure/Error: expect(article).to be_valid expected #<Article id: nil, user_id: 11, object: "e55pvzk0ni", price: 2759762496, store: "jycuh", content: "reuakjw8w54hvs5uagbo4ixn0cn8irkior7sz74uayxcjfp2dt", created_at: nil, updated_at: nil, rate: 1, category_id: 1> to be valid, but got errors: カテゴリを入力してください # ./spec/models/article_spec.rb:28:in `block (3 levels) in <top (required)>' Finished in 1.48 seconds (files took 6.55 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/models/article_spec.rb:27 # Article 全ての項目が入力されている場合 記事一覧を保存できる

各種記述ファイル

artice_spec.rb

require 'rails_helper' RSpec.describe 'Article', type: :model do let!(:user) { create(:user) } context '全ての項目が入力されている場合' do let!(:article) { build(:article, user: user) } it '記事を保存できる' do expect(article).to be_valid end end end

articles.rb Rspec

FactoryBot.define do factory :article do object { Faker::Lorem.characters(number: 10) } price { Faker::Number.number(1) } store { Faker::Lorem.characters(number: 5) } content { Faker::Lorem.characters(number: 50) } rate { 1 } category_id { 1 } end end

article.rb

class Article < ApplicationRecord belongs_to :category validates :category_id, presence: true end

category.rb

class Category < ApplicationRecord has_many :articles validates :category_name, presence: true end

schema.rb

create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "user_id", null: false t.string "object", null: false t.integer "price", null: false t.string "store", null: false t.string "content", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "rate" t.integer "category_id", null: false t.index ["category_id"], name: "index_articles_on_category_id" t.index ["user_id"], name: "index_articles_on_user_id" end create_table "categories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "category_name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end

#考えたこと
アソシエーション関連の記述が足りていないからなのかなとも思うのですがちょっとわからないのでわかる方ご教授お願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

class Article < ApplicationRecord belongs_to :category
ですので、Article作成時に category_id が必須です。
で、かつ その category_id は実在しなければなりません。

Rspecのcodeでは categor_id に 1を入れていますが、その前にcategoryが作られていない、つまり実在していない ので saveに失敗しています

投稿2021/03/27 00:43

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ik_ko

2021/03/27 01:07

ご回答ありがとうございます。 ご指摘の後に確かにその通りだと思った所見です。 ありがとうございます。 以下のように記述したところ解決いたしました。 article_spec ``` require 'rails_helper' RSpec.describe 'Article', type: :model do let!(:user) { create(:user) } let!(:category) { create(:category) } context '全ての項目が入力されている場合' do let!(:article) { build(:article, user: user, category: category ) } it '記事を保存できる' do expect(article).to be_valid end end end ``` categorys.rb ``` FactoryBot.define do factory :category do category_name { 'アイテム' } end end ```
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問