Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cover form part of ActiveAdmin resource with RSpec tests?

I've checked my coverage file, built by simplecov and I've seen uncovered area like this:

   form do |f|
     inputs 'Details' do
       input :email, required: true
       input :password, input_html: { value: t('empty_string') }, required: true
       input :is_active, label: t('active')
       input :slug
       input :name
       input :logo, as: :file
     end
     actions
   end

How can I deal with this ?

UPDATE: The render_views can help to avoid uncovered area.

like image 516
dsounded Avatar asked Nov 19 '25 07:11

dsounded


1 Answers

For SimpleCov to detect coverage of ActiveAdmin's form DSL, you need to create a controller test for the edit action. It would be a good idea to add coverage for the create and update actions to make sure permitted_params is not filtering specific attributes. I've included specs for the edit and update actions for the AdminUser model.

# spec/controllers/admin/users_controller_spec.rb
require 'rails_helper'
include Devise::TestHelpers

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

  let(:admin_user) { AdminUser.create!(email: '[email protected]', password: 'password')}

  before(:each) do
    sign_in admin_user
  end

  describe 'edit' do
    it 'renders user form' do
      get :edit, id: admin_user.to_param
      expect(assigns(:admin_user)).to eq admin_user
    end
  end

  describe 'update' do
    it 'updates user' do
      patch :update, { id: admin_user.to_param, admin_user: { email: '[email protected]' }}
      admin_user.reload
      expect(admin_user.email).to eq '[email protected]'
    end
  end
end
like image 114
scarver2 Avatar answered Nov 20 '25 21:11

scarver2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!