0
votes

I am building a Rails application with ActiveAdmin to handle the administration side.
I have defined some batch_action in my admin controller and I try now to make some tests to ensure it's properly working.
However, I don't find any informations about that: I've also been looking directly in the source code of ActiveAdmin without any luck.

Here are some extract of my code:

# Admin file
ActiveAdmin.register Event do
  batch_action :toggle_online do |ids|
    Event.find(ids).each { |item| item.toggle! :online }
    redirect_to :back, notice: t('active_admin.batch_actions.flash')
  end
end


# Test file
require 'test_helper'
module Admin
  class EventsControllerTest < ActionController::TestCase
    test 'should test toggle_online batch action' do
      post :batch_action, batch_action: 'toggle_online',  collection_selection: [@event.id.to_s] # not sure about this line
      assert_not assigns(:event).online? # assigns(:event) is nil
      assert_equal I18n.t('active_admin.batch_actions.flash'), flash[:notice]
      assert_redirected_to admin_events_path
    end
  end
end

Thanks for your help !

My project
- Rails 4.2.5.1
- Ruby 2.2.2
- ActiveAdmin 1.0.0pre2
- Rails Minitest (not rspec)

1
You do not have to test gem's functionality. - Andrey Deineko

1 Answers

0
votes

I finally found the solution: assigns(:event) is nil because it's inside the loop.
Here is what I did:

test 'should test toggle_online batch action' do
    post :batch_action, batch_action: 'toggle_online', collection_selection: [@event.id]
    @event.reload # This is important
    assert_not @event.online?
    assert_equal I18n.t('active_admin.batch_actions.flash'), flash[:notice]
    assert_redirected_to admin_events_path
end