-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from ShinJaehun/master
Posts Controller, Comments Controller 및 Spec 작성
- Loading branch information
Showing
6 changed files
with
362 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class CommentsController < ApplicationController | ||
|
||
skip_before_filter :authenticate_user! | ||
|
||
before_action :set_post | ||
before_action :set_comment, only: [:show, :update, :destroy] | ||
|
||
def index | ||
@comments = Comment.all | ||
render json: @comments | ||
end | ||
|
||
def show | ||
render json: @comment | ||
end | ||
|
||
def create | ||
@comment = @post.comments.create(comment_params) | ||
render json: @comment | ||
|
||
end | ||
|
||
def update | ||
@comment.update(comment_params) | ||
render json: @comment | ||
end | ||
|
||
def destroy | ||
@comment.destroy | ||
render json: { message: "destoryed" } | ||
end | ||
|
||
private | ||
def set_post | ||
@post = Post.find(params[:post_id]) | ||
end | ||
|
||
def set_comment | ||
@comment = @post.comments.find(params[:id]) | ||
end | ||
|
||
def comment_params | ||
params.require(:comment).permit(:content) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class PostsController < ApplicationController | ||
|
||
skip_before_filter :authenticate_user! | ||
|
||
before_action :set_post, only: [:show, :update, :destroy] | ||
|
||
def index | ||
@posts = Post.all | ||
render json: @posts | ||
end | ||
|
||
def show | ||
render json: @post | ||
end | ||
|
||
def create | ||
@post = Post.create(post_params) | ||
render json: @post | ||
end | ||
|
||
def update | ||
@post.update(post_params) | ||
render json: @post | ||
end | ||
|
||
def destroy | ||
@post.destroy | ||
render json: { message: "destroyed" } | ||
end | ||
|
||
private | ||
def set_post | ||
@post = Post.find(params[:id]) | ||
end | ||
|
||
def post_params | ||
params.require(:post).permit(:title, :content) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
require 'spec_helper' | ||
|
||
describe CommentsController do | ||
|
||
let(:post_create) { create(:post) } | ||
let(:comment) { create(:comment_to_post, commentable: post_create) } | ||
let(:valid_attributes) { attributes_for(:comment) } | ||
let(:invalid_attributes) { attributes_for(:comment, content: nil) } | ||
|
||
describe "GET #index" do | ||
it "> 모든 Comment들을 @comments에 할당한다." do | ||
comments = [comment, create(:comment_to_post, commentable: post_create)] | ||
get :index, post_id: post_create.id | ||
expect(assigns(:post)).to eq(post_create) | ||
expect(assigns(:comments)).to match_array(comments) | ||
end | ||
end | ||
|
||
describe "GET #show" do | ||
it "> 요청한 Comment로 instance 변수를 할당한다." do | ||
get :show, post_id: post_create.id, id: comment | ||
expect(assigns(:post)).to eq(post_create) | ||
expect(assigns(:comment)).to eq comment | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
context "1) params가 유효할 때" do | ||
def do_post | ||
post :create, post_id: post_create.id, comment: valid_attributes | ||
end | ||
|
||
it "> 새로운 Comment를 생성한다." do | ||
expect { | ||
do_post | ||
}.to change(Comment, :count).by(1) | ||
end | ||
|
||
it "> Comment instance 변수를 할당한다." do | ||
do_post | ||
expect(assigns(:comment)).to be_a(Comment) | ||
expect(assigns(:comment)).to_not be_new_record | ||
end | ||
end | ||
|
||
context "2) params가 유효하지 않을 때" do | ||
def do_post | ||
post :create, post_id: post_create.id, comment: invalid_attributes | ||
end | ||
|
||
it "> 새로운 Comment를 생성하지 않는다." do | ||
expect{ | ||
do_post | ||
}.to_not change(Comment, :count) | ||
end | ||
|
||
it "> 생성하지 못한 Comment로 instance 변수를 할당한다." do | ||
do_post | ||
expect(assigns(:comment)).to be_a(Comment) | ||
expect(assigns(:comment)).to be_new_record | ||
end | ||
end | ||
|
||
end | ||
|
||
describe 'PATCH #update' do | ||
context "1) params가 유효할 때" do | ||
def do_patch | ||
patch :update, post_id: post_create.id, id: comment, | ||
comment: attributes_for(:comment, content: "Answer content changed") | ||
end | ||
|
||
it "> 요청한 Comment를 업데이트한다." do | ||
do_patch | ||
comment.reload | ||
expect(comment.content).to eq "Answer content changed" | ||
end | ||
|
||
it "> 요청한 Post와 Comment로 instance 변수를 할당한다." do | ||
do_patch | ||
comment.reload | ||
expect(assigns(:post)).to eq(post_create) | ||
expect(assigns(:comment)).to eq comment | ||
end | ||
end | ||
|
||
context "2) params가 유효하지 않을 때" do | ||
def do_patch | ||
patch :update, post_id: post_create.id, id: comment, comment: invalid_attributes | ||
end | ||
|
||
it "> 요청한 Comment를 업데이트하지 않는다." do | ||
do_patch | ||
comment.reload | ||
expect(comment.content).to eq "abc edf" | ||
end | ||
|
||
it "> 요청한 Post와 Comment로 instance 변수를 할당한다." do | ||
do_patch | ||
comment.reload | ||
expect(assigns(:post)).to eq(post_create) | ||
expect(assigns(:comment)).to eq comment | ||
end | ||
end | ||
end | ||
|
||
describe 'DELETE #destroy' do | ||
before(:each) do | ||
@comment = create(:comment, commentable: post_create) | ||
end | ||
|
||
def do_delete | ||
delete :destroy, post_id: post_create.id, id: @comment | ||
end | ||
|
||
it "> 요청한 Comment를 삭제한다." do | ||
expect { | ||
do_delete | ||
}.to change(Comment, :count).by(-1) | ||
end | ||
|
||
it "> 요청한 Post와 삭제된 Comment로 instance 변수를 할당한다." do | ||
do_delete | ||
expect(assigns(:post)).to eq(post_create) | ||
expect(assigns(:comment)).to eq(@comment) | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.