15

I am trying to get started in testing ActiveAdmin, in particular I need to test a member_action from one of the ActiveAdmin controllers.

Do you guys know any good tutorials on this topic?

Thank you,

Calin
  • 6,661
  • 7
  • 49
  • 80

3 Answers3

21

This is the way I did it that worked for me:-

app/admin/posts.rb

ActiveAdmin.register Post do

  menu :parent => "Admin"


  #path = /admin/posts/:id/comments
  member_action :comments do
   @post = Post.find(params[:id])
  end 
end

spec/controllers/admin/posts_controller_spec.rb

require 'spec_helper'
include Devise::TestHelpers


describe Admin::PostsController do
  render_views

  before(:each) do
    @user = mock_model(User, :email => "tester@localspecs.com")
    request.env['tester'] = mock(Tester, :authenticate => @user, :authenticate! => @user)
  end

  describe "Get comments" do
    before(:each) do
      @post = Post.create! valid_attributes
      Post.should_receive(:find).at_least(:once).and_return(@post)
      get :comments, :id => @post.id
    end

    after(:each) do
      @post.destroy
    end

    it "gets the proper record to update" do
      assigns(:post).should eq(@post)
    end

    it "should render the actual template" do
      response.should contain("Comments")
      response.body.should =~ /Comments/m
    end
  end
end
Kiali
  • 226
  • 2
  • 2
6
# app/admin/post.rb
ActiveAdmin.register Post do
end

# spec/controller/admin/posts_controller_spec.rb
describe Admin::PostsController do
  subject { get :index }
  its(:status) { should eq 200 }
end
Dorian
  • 22,759
  • 8
  • 120
  • 116
  • 2
    I'm having this error: `uninitialized constant Admin (NameError)` Please, help. :) – across Feb 22 '16 at 12:24
  • 2
    @across That's because your namespace might be different, or not loaded. Maybe you are using `spec/spec_helper.rb` instead of `spec/rails_helper.rb` (a new stack overflow question would be the way to go to get it solved). – Dorian May 17 '17 at 17:31
2

Answer for 2022 and Rails v7

app/admin/dashboard.rb

ActiveAdmin.register_page 'Dashboard' do
  menu priority: 1

  content do
    div class: 'blank_slate_container', id: 'dashboard_default_message' do
      span class: 'blank_slate' do
        span 'Welcome to the Admin'
      end
    end
  end
end

spec/admin/dashboard_controller_spec.rb

RSpec.describe Admin::DashboardController, type: :controller do
  render_views

  it 'redirects to login' do
    get :index

    expect(response).to redirect_to(new_admin_user_session_path)
  end

  context 'when logged in as admin' do
    let(:admin) { create(:admin_user) }

    before { sign_in(admin) }

    it 'renders page' do
      get :index

      expect(response.body).to include('Welcome to the Admin')
    end
  end
end
thisismydesign
  • 21,553
  • 9
  • 123
  • 126