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
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ group :development, :test do
gem 'spring'

end

group :test do
gem "shoulda-matchers", '2.6.0'
gem "minitest"
end
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Answer < ActiveRecord::Base
belongs_to :quiz

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.present? && right_or_wrong && quiz.answers.where(right_or_wrong: true) > 0
error.add(:right_answer_number, "1つの問題に対して正解は1つだけです")
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20160317000000_create_answers.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,5 +49,6 @@
t.datetime "updated_at", null: false
end

add_foreign_key "answers", "quizzes"
add_foreign_key "quizzes", "quiz_titles"
end
7 changes: 7 additions & 0 deletions spec/factories/answers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :answer do
answer_text "MyString"
right_or_wrong false
quiz_id nil
end
end
4 changes: 2 additions & 2 deletions spec/factories/quiz_titles.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryGirl.define do
factory :quiz_title do

title "MyString"
url "MyString"
end

end
3 changes: 1 addition & 2 deletions spec/factories/quizzes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FactoryGirl.define do
factory :quiz do
quiz_statement "MyString"
quiz_title nil
quiz_title nil
end

end
25 changes: 25 additions & 0 deletions spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

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, right_or_wrong: false)
end

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) }
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
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down