Rails 测试 ActiveAdmin batch_action
Rails test ActiveAdmin batch_action
我正在使用 ActiveAdmin 构建一个 Rails 应用程序来处理管理方面的问题。
我已经在我的管理控制器中定义了一些 batch_action,我现在尝试进行一些测试以确保它正常工作。
但是,我没有找到任何相关信息:我也一直在直接查看 ActiveAdmin 的源代码,但没有任何运气。
这是我的代码的一些摘录:
# 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
感谢您的帮助!
我的项目
- Rails 4.2.5.1
- Ruby 2.2.2
- ActiveAdmin 1.0.0pre2
- Rails Minitest(不是 rspec)
我终于找到了解决方案:assigns(:event)
是 nil 因为它在循环内。
这是我所做的:
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
我正在使用 ActiveAdmin 构建一个 Rails 应用程序来处理管理方面的问题。
我已经在我的管理控制器中定义了一些 batch_action,我现在尝试进行一些测试以确保它正常工作。
但是,我没有找到任何相关信息:我也一直在直接查看 ActiveAdmin 的源代码,但没有任何运气。
这是我的代码的一些摘录:
# 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
感谢您的帮助!
我的项目
- Rails 4.2.5.1
- Ruby 2.2.2
- ActiveAdmin 1.0.0pre2
- Rails Minitest(不是 rspec)
我终于找到了解决方案:assigns(:event)
是 nil 因为它在循环内。
这是我所做的:
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