-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Comments controller destroy declaration (#6482)
* Fix the comments controller methods The destroy was defined in the create method 🤪 * Add tests for full coverage --------- Co-authored-by: Javier Julio <jjfutbol@gmail.com>
- Loading branch information
1 parent
b42c7f3
commit fc6fd8b
Showing
2 changed files
with
61 additions
and
8 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
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,53 @@ | ||
# frozen_string_literal: true | ||
require "rails_helper" | ||
|
||
RSpec.describe "ActiveAdmin Comments", type: :controller do | ||
before do | ||
load_resources { ActiveAdmin.register ActiveAdmin::Comment, as: "Comment" } | ||
@controller = Admin::CommentsController.new | ||
end | ||
|
||
describe "#destroy" do | ||
let(:user) { User.create! } | ||
let(:comment) { ActiveAdmin::Comment.create!(body: "body", namespace: :admin, resource: user, author: user) } | ||
|
||
context "success" do | ||
it "deletes comments and redirects to root fallback" do | ||
delete :destroy, params: { id: comment.id } | ||
|
||
expect(response).to redirect_to admin_root_url | ||
end | ||
|
||
it "deletes comments and redirects back" do | ||
request.env["HTTP_REFERER"] = "/admin/users/1" | ||
|
||
delete :destroy, params: { id: comment.id } | ||
|
||
expect(response).to redirect_to "/admin/users/1" | ||
end | ||
end | ||
|
||
context "failure" do | ||
it "does not delete comment on error and redirects to root fallback" do | ||
expect(@controller).to receive(:destroy_resource) do |comment| | ||
comment.errors.add(:body, :invalid) | ||
end | ||
|
||
delete :destroy, params: { id: comment.id } | ||
|
||
expect(response).to redirect_to admin_root_url | ||
end | ||
|
||
it "does not delete comment on error and redirects back" do | ||
request.env["HTTP_REFERER"] = "/admin/users/1" | ||
expect(@controller).to receive(:destroy_resource) do |comment| | ||
comment.errors.add(:body, :invalid) | ||
end | ||
|
||
delete :destroy, params: { id: comment.id } | ||
|
||
expect(response).to redirect_to "/admin/users/1" | ||
end | ||
end | ||
end | ||
end |