From 67fd7061debe9c4d1774436936ee7fa01eaaaba1 Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 17 Mar 2016 12:07:14 +0000 Subject: [PATCH 1/7] refine answer model --- app/models/answer.rb | 14 ++++++++++++++ db/migrate/20160317000000_create_answers.rb | 11 +++++++++++ spec/factories/answers.rb | 7 +++++++ spec/models/answer_spec.rb | 5 +++++ 4 files changed, 37 insertions(+) create mode 100644 app/models/answer.rb create mode 100644 db/migrate/20160317000000_create_answers.rb create mode 100644 spec/factories/answers.rb create mode 100644 spec/models/answer_spec.rb diff --git a/app/models/answer.rb b/app/models/answer.rb new file mode 100644 index 0000000..0aee262 --- /dev/null +++ b/app/models/answer.rb @@ -0,0 +1,14 @@ +class Answer < ActiveRecord::Base + belongs_to :quiz + + validates :answer, presence: true, length: { in: 1..30 } + validates :right_or_wrong, presence: true + 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 + error.add(:right_answer_number, "1つの問題に対して正解は1つだけです") + end + end +end diff --git a/db/migrate/20160317000000_create_answers.rb b/db/migrate/20160317000000_create_answers.rb new file mode 100644 index 0000000..ce5eca8 --- /dev/null +++ b/db/migrate/20160317000000_create_answers.rb @@ -0,0 +1,11 @@ +class CreateAnswers < ActiveRecord::Migration + def change + create_table :answers do |t| + t.string :answer_text, :null => false + t.boolean :right_or_wrong, :null => false + t.belongs_to :quiz, index: true, foreign_key: true + + t.timestamps null: false + end + end +end diff --git a/spec/factories/answers.rb b/spec/factories/answers.rb new file mode 100644 index 0000000..fbd4127 --- /dev/null +++ b/spec/factories/answers.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :answer do + answer_text "MyString" +  right_or_wrong false +  quiz_id nil + end +end diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb new file mode 100644 index 0000000..2cf7bf6 --- /dev/null +++ b/spec/models/answer_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Answer, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From c1b1df948f47d8982888a3f57a78e62ee17665b9 Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 17 Mar 2016 12:33:41 +0000 Subject: [PATCH 2/7] refine spec/factories/answers.rb --- spec/factories/answers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/factories/answers.rb b/spec/factories/answers.rb index fbd4127..95737f9 100644 --- a/spec/factories/answers.rb +++ b/spec/factories/answers.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :answer do answer_text "MyString" -  right_or_wrong false -  quiz_id nil + right_or_wrong false + quiz_id nil end end From d284ad5d09af5aaadc0d11f327dc2e3ab83b854b Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 17 Mar 2016 13:03:33 +0000 Subject: [PATCH 3/7] answer-rspec --- db/schema.rb | 13 ++++++++++++- spec/factories/quiz_titles.rb | 4 ++-- spec/factories/quizzes.rb | 3 +-- spec/models/answer_spec.rb | 29 ++++++++++++++++++++++++++++- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index c8bc635..a97318c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160311000000) do +ActiveRecord::Schema.define(version: 20160317000000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "answers", force: :cascade do |t| + t.string "answer_text", null: false + t.boolean "right_or_wrong", null: false + t.integer "quiz_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "answers", ["quiz_id"], name: "index_answers_on_quiz_id", using: :btree + create_table "quiz_titles", force: :cascade do |t| t.string "title", null: false t.text "url", null: false @@ -39,5 +49,6 @@ t.datetime "updated_at", null: false end + add_foreign_key "answers", "quizzes" add_foreign_key "quizzes", "quiz_titles" end diff --git a/spec/factories/quiz_titles.rb b/spec/factories/quiz_titles.rb index a1bed9a..07497d4 100644 --- a/spec/factories/quiz_titles.rb +++ b/spec/factories/quiz_titles.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :quiz_title do - + title "MyString" + url "MyString" end - end diff --git a/spec/factories/quizzes.rb b/spec/factories/quizzes.rb index 3714c2a..77afde0 100644 --- a/spec/factories/quizzes.rb +++ b/spec/factories/quizzes.rb @@ -1,7 +1,6 @@ FactoryGirl.define do factory :quiz do quiz_statement "MyString" -quiz_title nil + quiz_title nil end - end diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index 2cf7bf6..8ae64b1 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -1,5 +1,32 @@ require 'rails_helper' RSpec.describe Answer, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + 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) + end + + subject { @answer1 } + + it { should respond_to(:answer_text) } + # it { should respond_to(:right_or_wrong) } + # it { should respond_to(:quiz_id) } + # + # it { should be_valid } + # + # describe "when answer_text is not present" do + # before { @answer1.answer_text = " " } + # it { should_not be_valid } + # end + # + # describe "when right_or_wrong is not present" do + # before { @answer1.right_or_wrong = nil } + # it { should_not be_valid } + # end + # + # describe "when quiz_id is not present" do + # before { @answer1.quiz_id = nil } + # it { should_not be_valid } + # end end From 1028156d41e436978c15b35df926d2b9e2a685fa Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 31 Mar 2016 12:26:33 +0000 Subject: [PATCH 4/7] resolve error --- app/models/answer.rb | 5 ++--- spec/models/answer_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/models/answer.rb b/app/models/answer.rb index 0aee262..0348afe 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -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 diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index 8ae64b1..a29394a 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -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 } From 4952ae4a61f3101a376be3f5362a577617e6d286 Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 31 Mar 2016 12:44:29 +0000 Subject: [PATCH 5/7] rspec shoulda-matchers --- Gemfile | 5 +++++ Gemfile.lock | 4 ++++ spec/rails_helper.rb | 2 ++ 3 files changed, 11 insertions(+) diff --git a/Gemfile b/Gemfile index 91d17e8..b7b6f37 100644 --- a/Gemfile +++ b/Gemfile @@ -49,3 +49,8 @@ group :development, :test do gem 'spring' end + +group :test do + gem "shoulda-matchers", '2.6.0' + gem "minitest" +end diff --git a/Gemfile.lock b/Gemfile.lock index c53d616..171a721 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -137,6 +137,8 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + shoulda-matchers (2.6.0) + activesupport (>= 3.0.0) skyblue-rails (0.0.0.2) railties (>= 3.1.0) spring (1.6.2) @@ -172,11 +174,13 @@ DEPENDENCIES factory_girl_rails jbuilder (~> 2.0) jquery-rails + minitest pg rails (= 4.2.3) rspec-rails sass-rails (~> 5.0) sdoc (~> 0.4.0) + shoulda-matchers (= 2.6.0) skyblue-rails spring turbolinks diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 88ff2d0..fdd0b5f 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -5,6 +5,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' require 'rspec/rails' +require 'minitest/autorun' +require 'shoulda/matchers' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in From ec9ce50f817fdf68a40f913658c014b94afef2f0 Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 31 Mar 2016 12:52:22 +0000 Subject: [PATCH 6/7] refine answer model rspec --- spec/models/answer_spec.rb | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index a29394a..3b22e5c 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -9,24 +9,13 @@ subject { @answer1 } - it { should respond_to(:answer_text) } - # it { should respond_to(:right_or_wrong) } - # it { should respond_to(:quiz_id) } - # - # it { should be_valid } - # - # describe "when answer_text is not present" do - # before { @answer1.answer_text = " " } - # it { should_not be_valid } - # end - # - # describe "when right_or_wrong is not present" do - # before { @answer1.right_or_wrong = nil } - # it { should_not be_valid } - # end - # - # describe "when quiz_id is not present" do - # before { @answer1.quiz_id = nil } - # it { should_not be_valid } - # end + describe "answer_text validate" do + it { should validate_presence_of(:answer_text) } + it { should ensure_length_of(:answer_text).is_at_least(1) } + it { should ensure_length_of(:answer_text).is_at_most(30) } + end + + describe "answer_text validate" do + it { should validate_presence_of(:quiz_id) } + end end From 4f8d10d0ba28c2107c39e85b469a9d780409ca03 Mon Sep 17 00:00:00 2001 From: masahiro-yamazaki Date: Thu, 31 Mar 2016 12:55:35 +0000 Subject: [PATCH 7/7] refine answer model rspec --- spec/models/answer_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index 3b22e5c..440b5aa 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -9,6 +9,10 @@ subject { @answer1 } + describe "belongs_to quiz model" do + it { should belong_to(:quiz)} + end + describe "answer_text validate" do it { should validate_presence_of(:answer_text) } it { should ensure_length_of(:answer_text).is_at_least(1) }