Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer rspec(作成中) #32

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolve error
masahiro-yamazaki committed Mar 31, 2016
commit 1028156d41e436978c15b35df926d2b9e2a685fa
5 changes: 2 additions & 3 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class Answer < ActiveRecord::Base
belongs_to :quiz

validates :answer, presence: true, length: { in: 1..30 }
validates :right_or_wrong, presence: true
validates :answer_text, presence: true, length: { in: 1..30 }
validates :quiz_id, presence: true
validate :right_answer_is_only_one_by_one_quiz

def right_answer_is_only_one_by_one_quiz
if quiz.answers.where(right_or_wrong: true) > 1
if quiz.present? && right_or_wrong && quiz.answers.where(right_or_wrong: true) > 0
error.add(:right_answer_number, "1つの問題に対して正解は1つだけです")
end
end
4 changes: 2 additions & 2 deletions spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
RSpec.describe Answer, type: :model do
before do
@quiz_title1 = FactoryGirl.create(:quiz_title, id: 1)
@quiz1 = FactoryGirl.create(:quiz, id: 1 , quiz_title_id: 1)
@answer1 = FactoryGirl.create(:answer, id: 1, quiz_id: 1)
@quiz1 = FactoryGirl.create(:quiz, id: 1 , quiz_title_id: 1)
@answer1 = FactoryGirl.create(:answer, id: 1, quiz_id: 1, right_or_wrong: false)
end

subject { @answer1 }