2015-07-16 11:57:27 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe CommentsController, type: :controller do
|
|
|
|
let!(:user) { build(:user) }
|
|
|
|
let!(:subcreddit) { create(:subcreddit) }
|
|
|
|
let!(:tpost) { create(:post, subcreddit: subcreddit) }
|
|
|
|
let(:data) { { content: 'Here is some updated content for a comment' } }
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
allow_any_instance_of(ApplicationController)
|
|
|
|
.to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show' do
|
|
|
|
let!(:comment) { create(:comment) }
|
|
|
|
before(:each) do
|
|
|
|
get :show,
|
|
|
|
subcreddit_id: comment.post.subcreddit,
|
|
|
|
post_id: comment.post,
|
|
|
|
id: comment
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render :show' do
|
|
|
|
expect(response).to render_template(:show)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should assign correct Comment to @comment' do
|
|
|
|
expect(assigns(:comment)).to eq(comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#new' do
|
|
|
|
let(:comment) { build(:comment) }
|
|
|
|
before(:each) do
|
|
|
|
get :new,
|
|
|
|
subcreddit_id: comment.post.subcreddit,
|
|
|
|
post_id: comment.post
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render :new' do
|
|
|
|
expect(response).to render_template(:new)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should assign new Comment to @comment' do
|
|
|
|
expect(assigns(:comment)).to be_a_new(Comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#create' do
|
|
|
|
context 'with valid data' do
|
|
|
|
it 'should create a comment' do
|
|
|
|
expect do
|
|
|
|
post :create,
|
|
|
|
subcreddit_id: subcreddit,
|
|
|
|
post_id: tpost,
|
|
|
|
comment: data
|
|
|
|
end.to change(Comment, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should redirect to the parent post' do
|
|
|
|
expect(post :create,
|
|
|
|
subcreddit_id: subcreddit,
|
|
|
|
post_id: tpost,
|
|
|
|
comment: data
|
|
|
|
).to redirect_to(subcreddit_post_path(assigns(:post).subcreddit,
|
|
|
|
assigns(:post)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send a notice flash message' do
|
|
|
|
expect(post :create,
|
|
|
|
subcreddit_id: subcreddit,
|
|
|
|
post_id: tpost,
|
|
|
|
comment: data)
|
|
|
|
|
|
|
|
expect(flash[:notice]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid data' do
|
|
|
|
before(:each) { data['content'] = '' }
|
|
|
|
|
|
|
|
it 'should not create a new comment' do
|
|
|
|
expect do
|
|
|
|
post :create,
|
|
|
|
subcreddit_id: subcreddit,
|
|
|
|
post_id: tpost,
|
|
|
|
comment: data
|
|
|
|
end.to change(Comment, :count).by(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render :new' do
|
|
|
|
expect(post :create,
|
|
|
|
subcreddit_id: subcreddit,
|
|
|
|
post_id: tpost,
|
|
|
|
comment: data
|
|
|
|
).to redirect_to(subcreddit_post_path(subcreddit, tpost))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#edit' do
|
2015-12-14 13:28:39 -05:00
|
|
|
context 'when owner' do
|
|
|
|
let!(:comment) { create(:comment, user: user) }
|
|
|
|
before(:each) do
|
|
|
|
get :edit,
|
|
|
|
id: comment,
|
|
|
|
post_id: comment.post,
|
|
|
|
subcreddit_id: comment.post.subcreddit
|
2015-07-16 11:57:27 -04:00
|
|
|
end
|
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
context 'with valid comment' do
|
|
|
|
it 'should render :edit' do
|
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should assign correct Comment to @comment' do
|
|
|
|
expect(assigns(:comment)).to eq(comment)
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '#update' do
|
|
|
|
let(:data) { { content: 'Some edited comment content goes here' } }
|
|
|
|
|
|
|
|
context 'with valid data' do
|
2015-12-14 13:28:39 -05:00
|
|
|
let!(:comment) { create(:comment, user: user) }
|
|
|
|
|
|
|
|
context 'when owner' do
|
|
|
|
before(:each) do
|
|
|
|
put :update,
|
|
|
|
id: comment,
|
|
|
|
post_id: comment.post,
|
|
|
|
subcreddit_id: comment.post.subcreddit,
|
|
|
|
comment: data
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should assign correct Comment to @comment' do
|
|
|
|
expect(assigns(:comment)).to eq(comment)
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should update the comment' do
|
|
|
|
comment.reload
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
expect(comment.content).to eq(data[:content])
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should redirect to the post' do
|
|
|
|
expect(response)
|
|
|
|
.to redirect_to(subcreddit_post_path(assigns(:post).subcreddit,
|
|
|
|
assigns(:post)))
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should display a notice flash message' do
|
|
|
|
expect(flash[:notice]).to be_present
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
end
|
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
context 'with invalid data' do
|
|
|
|
before(:each) { data[:content] = '' }
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should render :edit' do
|
|
|
|
put :update,
|
|
|
|
id: comment,
|
|
|
|
post_id: comment.post,
|
|
|
|
subcreddit_id: comment.post.subcreddit,
|
|
|
|
comment: data
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '#destroy' do
|
2015-12-14 13:28:39 -05:00
|
|
|
context 'when owner' do
|
|
|
|
let!(:comment) { create(:comment, user: user) }
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should delete the post' do
|
|
|
|
delete :destroy,
|
|
|
|
id: comment,
|
|
|
|
post_id: comment.post,
|
|
|
|
subcreddit_id: comment.post.subcreddit
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
comment.reload
|
|
|
|
expect(comment.destroyed?).to be(true)
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should redirect to the post' do
|
|
|
|
delete :destroy,
|
|
|
|
id: comment,
|
|
|
|
post_id: comment.post,
|
|
|
|
subcreddit_id: comment.post.subcreddit
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
expect(response)
|
|
|
|
.to redirect_to(subcreddit_post_path(assigns(:subcreddit),
|
|
|
|
assigns(:post)))
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
it 'should send a notice flash message' do
|
|
|
|
delete :destroy,
|
|
|
|
id: comment,
|
|
|
|
post_id: comment.post,
|
|
|
|
subcreddit_id: comment.post.subcreddit
|
|
|
|
expect(flash[:notice]).to be_present
|
|
|
|
end
|
2015-07-16 11:57:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|