From 7d08faf22bb2d599d99a69db5b6dc6cc07bb9e0e Mon Sep 17 00:00:00 2001 From: shiotomo Date: Wed, 20 Mar 2019 22:50:53 +0900 Subject: [PATCH] =?UTF-8?q?model=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/question.rb | 2 +- spec/models/answer_spec.rb | 12 +++++++++++- spec/models/category_item_spec.rb | 14 +++++++++++++- spec/models/category_spec.rb | 13 ++++++++++++- spec/models/code_spec.rb | 9 ++++++++- spec/models/question_spec.rb | 19 +++++++++++++++---- spec/models/result_spec.rb | 15 ++++++++++++++- spec/models/tweet_spec.rb | 8 ++++---- spec/models/user_spec.rb | 1 - 9 files changed, 78 insertions(+), 15 deletions(-) diff --git a/app/models/question.rb b/app/models/question.rb index 8b8c153..f998a75 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -10,7 +10,7 @@ # class Question < ApplicationRecord - validates :title, {presence: true, uniqueness: true} + validates :title, { presence: true, uniqueness: true } has_many :results, dependent: :destroy, inverse_of: :question has_many :answers, dependent: :destroy, inverse_of: :question diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index 9af3bb6..bd40458 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -13,5 +13,15 @@ require 'rails_helper' RSpec.describe Answer, type: :model do - # pending "add some examples to (or delete) #{__FILE__}" + before do + @question = create(:question, id: 1) + end + + it 'データを登録できる' do + answer = @question.answers.new( + input: "", + output: "hoge" + ) + expect(answer).to(be_valid) + end end diff --git a/spec/models/category_item_spec.rb b/spec/models/category_item_spec.rb index 0249e61..66932d1 100644 --- a/spec/models/category_item_spec.rb +++ b/spec/models/category_item_spec.rb @@ -12,5 +12,17 @@ require 'rails_helper' RSpec.describe CategoryItem, type: :model do - # pending "add some examples to (or delete) #{__FILE__}" + before do + @question = create(:question, id: 1) + @category = create(:category, id: 1) + end + + it 'データを登録できる' do + category_item = CategoryItem.new( + question_id: @question.id, + category_id: @category.id + ) + expect(category_item).to(be_valid) + end + end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 7903904..482d109 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -12,5 +12,16 @@ require 'rails_helper' RSpec.describe Category, type: :model do - # pending "add some examples to (or delete) #{__FILE__}" + it 'データを登録できる' do + category = create(:category, id: 1) + expect(category).to(be_valid) + end + + it 'タイトルがない場合データを登録できない' do + category = Question.new( + title: '', + body: '' + ) + expect(category).not_to(be_valid) + end end diff --git a/spec/models/code_spec.rb b/spec/models/code_spec.rb index 89d9529..539dffd 100644 --- a/spec/models/code_spec.rb +++ b/spec/models/code_spec.rb @@ -13,5 +13,12 @@ require 'rails_helper' RSpec.describe Code, type: :model do - # pending "add some examples to (or delete) #{__FILE__}" + it 'データをレコードできる' do + user = create(:user, id: 1) + code = user.codes.new( + code: 'puts 1', + language: 'Ruby' + ) + expect(code).to(be_valid) + end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index e80df1c..5121bcc 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -13,12 +13,23 @@ RSpec.describe Question, type: :model do before do - @question = FactoryBot.create(:question) end - context "問題を登録する時" do - it "問題文だけ正常に登録できる" do - @question.save + context '問題を登録する時' do + it '問題文だけ正常に登録できる' do + question = Question.new( + title: 'hoge', + body: '' + ) + expect(question).to(be_valid) + end + + it 'タイトルが空の場合はレコードできない' do + question = Question.new( + title: '', + body: '' + ) + expect(question).not_to(be_valid) end end end diff --git a/spec/models/result_spec.rb b/spec/models/result_spec.rb index 179f956..4ea5db4 100644 --- a/spec/models/result_spec.rb +++ b/spec/models/result_spec.rb @@ -15,5 +15,18 @@ require 'rails_helper' RSpec.describe Result, type: :model do - # pending "add some examples to (or delete) #{__FILE__}" + before do + @question = create(:question, id: 1) + @user = create(:user, id: 1) + end + + it 'データをレコードできる' do + answer = @user.results.new( + answer: true, + question_id: @question.id, + code: 'puts 1', + language: 'Ruby' + ) + expect(answer).to(be_valid) + end end diff --git a/spec/models/tweet_spec.rb b/spec/models/tweet_spec.rb index e5f2214..47231b0 100644 --- a/spec/models/tweet_spec.rb +++ b/spec/models/tweet_spec.rb @@ -20,21 +20,21 @@ tweet = @user.tweets.create( body: "hoge", ) - expect(tweet).to be_valid + expect(tweet).to(be_valid) end it "bodyに値が入っていない時はエラー" do tweet = @user.tweets.create( body: nil ) - expect(tweet).not_to be_valid + expect(tweet).not_to(be_valid) end it "user_idに値が入っていない時はエラー" do tweet = Tweet.create( body: "hoge" ) - expect(tweet).not_to be_valid + expect(tweet).not_to(be_valid) end it "bodyの文字列が120文字以上の時はエラー" do @@ -45,6 +45,6 @@ tweet = @user.tweets.create( body: text, ) - expect(tweet).not_to be_valid + expect(tweet).not_to(be_valid) end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a26c977..27f83db 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -16,7 +16,6 @@ require 'rails_helper' RSpec.describe User, type: :model do - # pending "add some examples to (or delete) #{__FILE__}" it "ユーザを登録できる" do user = FactoryBot.create(:user) expect(user).to be_valid