Skip to content

Commit

Permalink
Merge pull request #72 from plaredspear/master
Browse files Browse the repository at this point in the history
Answer controller 생성 및 스펙 작성
  • Loading branch information
rorlab committed Apr 18, 2014
2 parents fcca7dd + dd77e76 commit 6ba903e
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 36 deletions.
39 changes: 39 additions & 0 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class AnswersController < ApplicationController
# 임시 테스트용
skip_before_filter :authenticate_user!

before_action :set_answer, only: [:show, :update, :destroy]

def index
@answers = Answer.all
render json: @answers
end

def show
render json: @answer
end

def create
@answer = Answer.create(answer_params)
render json: @answer
end

def update
@answer.update(answer_params)
render json: @answer
end

def destroy
@answer.destroy
render json: { message: "destroyed" }
end

private
def set_answer
@answer = Answer.find(params[:id])
end

def answer_params
params.require(:answer).permit(:title, :content)
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
resources :users, except: [ :new, :edit ]

resources :questions, only: [ :index, :show, :create, :update, :destroy ]
resources :answers, only: [ :index, :show, :create, :update, :destroy ]

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
93 changes: 93 additions & 0 deletions spec/controllers/answers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require 'spec_helper'

describe AnswersController do

let(:answer) { create(:answer) }
let(:valid_attributes) { attributes_for(:answer) }
let(:invalid_attributes) { attributes_for(:answer, content: nil) }

describe 'GET #index' do
it "> 모든 Answer로 instance 변수를 할당한다." do
answers = [answer, create(:answer)]
get :index
expect(assigns(:answers)).to match_array(answers)
end
end

describe 'GET #show' do
it "> 요청한 Answer로 instance 변수를 할당한다." do
get :show, id: answer
expect(assigns(:answer)).to eq answer
end
end

describe 'POST #create' do
context "1) params가 유효할 때" do
it "> 새로운 answer를 생성한다." do
expect{
post :create, answer: valid_attributes
}.to change(Answer, :count).by(1)
end

it "> 새로운 answer로 instance 변수를 할당한다." do
post :create, answer: valid_attributes
expect(assigns(:answer)).to be_a(Answer)
expect(assigns(:answer)).to_not be_new_record
end
end

context "2) params가 유효하지 않을 때" do
it "> 새로운 answer를 생성하지 않는다." do
expect{
post :create, answer: valid_attributes
}.to change(Answer, :count)
end

it "> 새로운 answer로 instance 변수를 할당한다." do
post :create, answer: invalid_attributes
expect(assigns(:answer)).to be_a(Answer)
expect(assigns(:answer)).to be_new_record
end
end
end

describe 'PATCH #update' do
context "1) params가 유효할 때" do
it "> 요청한 answer를 업데이트한다." do
patch :update, id: answer, answer: attributes_for(:answer, content: "Answer content changed")
answer.reload
expect(answer.content).to eq "Answer content changed"
end

it "> 요청한 answer로 instance 변수를 할당한다." do
patch :update, id: answer, answer: attributes_for(:answer, content: "Answer content changed")
answer.reload
expect(assigns(:answer)).to eq answer
end
end

context "2) params가 유효하지 않을 때" do
it "> 요청한 answer를 업데이트하지 않는다." do
patch :update, id: answer, answer: invalid_attributes
answer.reload
expect(answer.content).to eq "Answer Content"
end

it "> 요청한 answer로 instance 변수를 할당한다." do
patch :update, id: answer, answer: invalid_attributes
answer.reload
expect(assigns(:answer)).to eq answer
end
end
end

describe 'DELETE #destroy' do
it "> 요청한 answer를 삭제한다." do
answer
expect {
delete :destroy, id: answer
}.to change(Answer, :count).by(-1)
end
end

end
66 changes: 37 additions & 29 deletions spec/controllers/questions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

describe QuestionsController do

before :each do
@question = create(:question, title: "Question Title", content: "Question Content")
end
let(:question) { create(:question) }
let(:valid_attributes) { attributes_for(:question) }
let(:invalid_attributes) { attributes_for(:question, title: nil) }

describe 'GET #index' do
it "> 모든 Question을 @questions에 할당한다." do
question1 = create(:question, title: "Q1")
it "> 모든 Question으로 instance 변수를 할당한다." do
questions = [question, create(:question)]
get :index
expect(assigns(:questions)).to match_array([@question, question1])
expect(assigns(:questions)).to match_array(questions)
end
end

describe 'GET #show' do
it "> 요청한 Question을 question instance에 할당한다." do
get :show, id: @question
expect(assigns(:question)).to eq @question
it "> 요청한 Question으로 instance 변수를 할당한다." do
get :show, id: question
expect(assigns(:question)).to eq question
end
end

describe 'POST #create' do
context "1) params가 유효할 때" do
it "> 새로운 question을 생성한다." do
expect {
post :create, { question: attributes_for(:question) }
post :create, question: valid_attributes
}.to change(Question, :count).by(1)
end

it "> question instance에 할당한다." do
post :create, { question: attributes_for(:question) }
it "> 새로운 question으로 instance 변수를 할당한다." do
post :create, question: valid_attributes
expect(assigns(:question)).to be_a(Question)
expect(assigns(:question)).to_not be_new_record
end
Expand All @@ -39,12 +39,12 @@
context "2) params가 유효하지 않을 때" do
it "> 새로운 quesiton을 생성하지 않는다." do
expect {
post :create, { question: attributes_for(:question_invalid) }
post :create, question: invalid_attributes
}.to_not change(Question, :count)
end

it "> question instance에 할당한다." do
post :create, { question: attributes_for(:question_invalid) }
it "> 새로운 question을 instance 변수에 할당한다." do
post :create, question: invalid_attributes
expect(assigns(:question)).to be_a(Question)
expect(assigns(:question)).to be_new_record
end
Expand All @@ -54,33 +54,41 @@
describe 'PATCH #update' do
context "1) params가 유효할 때" do
it "> 요청한 question을 업데이트한다." do
patch :update, id: @question, question: attributes_for(:question, title: "Wonderfulday isn't it?", content: "Yes!!!")
@question.reload
expect(@question.title).to eq("Wonderfulday isn't it?")
expect(@question.content).to eq("Yes!!!")
patch :update, id: question, question: attributes_for(:question, title: "Wonderfulday isn't it?", content: "Yes!!!")
question.reload
expect(question.title).to eq("Wonderfulday isn't it?")
expect(question.content).to eq("Yes!!!")
end

it "> 요청한 question을 instance 변수에 할당한다." do
patch :update, id: question, question: valid_attributes
question.reload
expect(assigns(:question)).to eq question
end
end

context "2) params가 유효하지 않을 때" do
it "> 요청한 quesiton을 업데이트하지 않는다." do
patch :update, id: @question, question: attributes_for(:question_invalid)
@question.reload
expect(@question.title).to eq("Question Title")
expect(@question.content).to eq("Question Content")
patch :update, id: question, question: invalid_attributes
question.reload
expect(question.title).to eq("Question Title")
expect(question.content).to eq("Question Content")
end

it "> 요청한 question을 instance 변수에 할당한다." do
patch :update, id: question, question: invalid_attributes
question.reload
expect(assigns(:question)).to eq question
end
end

it "> question instance에 할당한다." do
patch :update, id: @question, question: attributes_for(:question_invalid)
@question.reload
expect(assigns(:question)).to eq @question
end
end

describe 'DELETE #destroy' do
it "> 요청한 question을 삭제한다." do
question
expect {
delete :destroy, id: @question
delete :destroy, id: question
}.to change(Question, :count).by(-1)
end
end
Expand Down
8 changes: 1 addition & 7 deletions spec/factories/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@

FactoryGirl.define do
factory :question do
title "Question title"
title "Question Title"
content "Question Content"


factory :question_invalid do
title nil
end

end
end

0 comments on commit 6ba903e

Please sign in to comment.