From d5fece87d0c54eff4c82cc02c887fb5946046e1b Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 26 Nov 2017 20:58:40 +1300 Subject: [PATCH] Tests for comments#index (and bugfix) --- app/controllers/comments_controller.rb | 2 +- spec/controllers/comments_controller_spec.rb | 30 +++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index d99c9c1eb..f85302d74 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 23210a875..0e986cc93 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -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