Skip to content

Commit

Permalink
modelのテストを追加した
Browse files Browse the repository at this point in the history
  • Loading branch information
shiotomo committed Mar 20, 2019
1 parent dbfbbb6 commit 7d08faf
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion spec/models/category_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion spec/models/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion spec/models/code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 15 additions & 4 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion spec/models/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions spec/models/tweet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +45,6 @@
tweet = @user.tweets.create(
body: text,
)
expect(tweet).not_to be_valid
expect(tweet).not_to(be_valid)
end
end
1 change: 0 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7d08faf

Please sign in to comment.