Tests for comments#index (and bugfix)

This commit is contained in:
Brenda Wallace
2017-11-26 20:58:40 +13:00
committed by Shiny
parent 33816d4312
commit d5fece87d0
2 changed files with 18 additions and 14 deletions

View File

@@ -6,7 +6,7 @@ class CommentsController < ApplicationController
responders :flash
def index
@comments = Comment.order("comments.created_at": :desc).paginate(page: params[:page])
@comments = Comment.order(created_at: :desc).paginate(page: params[:page])
respond_with(@comments)
end

View File

@@ -1,10 +1,11 @@
require 'rails_helper'
describe CommentsController do
subject { response }
let(:member) { FactoryBot.create(:member) }
before(:each) do
@member = FactoryBot.create(:member)
sign_in @member
controller.stub(:current_member) { @member }
sign_in member
controller.stub(:current_member) { member }
end
def valid_attributes
@@ -13,11 +14,14 @@ describe CommentsController do
end
describe "GET RSS feed" do
it "returns an RSS feed" do
get :index, format: "rss"
response.should be_success
response.should render_template("comments/index")
response.content_type.should eq("application/rss+xml")
let!(:first_comment) { FactoryBot.create :comment, created_at: 10.days.ago }
let!(:last_comment) { FactoryBot.create :comment, created_at: 4.minutes.ago }
describe "returns an RSS feed" do
before { get :index, format: "rss" }
it { is_expected.to be_success }
it { is_expected.to render_template("comments/index") }
it { expect(response.content_type).to eq("application/rss+xml") }
it { expect(assigns(:comments)).to eq([last_comment, first_comment]) }
end
end
@@ -48,10 +52,10 @@ describe CommentsController do
before { get :edit, id: comment.to_param }
describe "my comment" do
let!(:comment) { FactoryBot.create :comment, author: @member, post: post }
let!(:comment) { FactoryBot.create :comment, author: member, post: post }
let!(:old_comment) { FactoryBot.create(:comment, post: post, created_at: Time.zone.yesterday) }
it "assigns previous comments as @comments" do
assigns(:comments).should eq([comment, old_comment])
expect(assigns(:comments)).to eq([comment, old_comment])
end
end
@@ -65,7 +69,7 @@ describe CommentsController do
before { put :update, id: comment.to_param, comment: valid_attributes }
describe "my comment" do
let(:comment) { FactoryBot.create :comment, author: @member }
let(:comment) { FactoryBot.create :comment, author: member }
it "redirects to the comment's post" do
expect(response).to redirect_to(comment.post)
end
@@ -78,7 +82,7 @@ describe CommentsController do
let(:post) { FactoryBot.create :post, subject: 'our post' }
let(:other_post) { FactoryBot.create :post, subject: 'the other post' }
let(:valid_attributes) { { post_id: other_post.id, body: "kōrero" } }
let(:comment) { FactoryBot.create :comment, author: @member, post: post }
let(:comment) { FactoryBot.create :comment, author: member, post: post }
it "does not change post_id" do
comment.reload
expect(comment.post_id).to eq(post.id)
@@ -90,7 +94,7 @@ describe CommentsController do
before { delete :destroy, id: comment.to_param }
describe "my comment" do
let(:comment) { FactoryBot.create :comment, author: @member }
let(:comment) { FactoryBot.create :comment, author: member }
it "redirects to the post the comment was on" do
expect(response).to redirect_to(comment.post)
end