Active admin: Writing Rspec for custom CSV.

Active admin is a great framework for managing website admin side interface. Usually, UI for admin users needs to be more of managing and reporting the business operations than the fancy UI which we build for end-users. So basically Active admin abstracts common business applications in a simple pattern so that developers can use it and implement a beautiful interface with very little effort.

In one of our projects, we have built a feature for CSV export in Active admin. For that, I wanted to write Rspec test cases, as it was an important part for admins. While discussing the same with my team, we landed on the perspective that CSV is exported via gem then should we test it or not? As my feature was a core of business logic & is related to critical flow, I thought we should test it. Hence I added RSpec test cases around it. Let me explain how one can do the same if needed.

How to make custom CSV –

By default records that are being displayed on the screen are exported in CSV, XML, or JSON. But sometimes we need to download specific records coming from custom queries or scope. Let’s take an example. There can be many fields/columns in CSV, but we are considering only a few of them.

csv do  
  
  column :first_name
  column :last_name
  column :email
  column(:role) { |u| u.role_name&.humanize }  

end

So considering this code, I have kept a few points in my mind to test it correctly which are as below –

  1. Check the response format of CSV.
  2. Check CSV headers in a response.
  3. Check CSV data.

So let’s start with our first scenario: 

Rspec for custom CSV –

  1. Checking response format for CSV
it 'returns success if request format is CSV' do

  get :index, format: :csv
  expect(response).to have_http_status(:success)
  expect(response.header['Content-Type']).to include 'text/csv'

end

In the above scenario, we are requesting for CSV, and in response, we are checking the response type. That means requesting for CSV then in response should also have CSV.

2. Checking required headers

it ‘get expected headers in CSV’ do  

  get :index, format: :csv  
  csv_data = response.body.delete_prefix!(“\xEF\xBB\xBF”)
  csv_headers = CSV.parse(csv_data)[0]  
  expected_headers = [‘first_name’,    
    ‘last_name’    
    ‘Email’,    
    ‘Role’,  
  ]  

  expect(csv_headers).to match_array(expected_headers)

end

In the above scenario, we are checking what list of columns we will get in exported CSV. We know already a list of columns, so we are comparing our defined columns list and columns which are getting from CSV.

Now if you observe the following line then you may feel something different in RSpec. Right?

csv_data = response.body.delete_prefix!(“\xEF\xBB\xBF”)

So let’s understand what it is?

Byte Order Mapping(BOM) Concept –

As we can see, the characters(\xEF\xBB\xBF) which we are removed are hidden characters. According to Wikipedia these are hidden characters provided at the start at the text stream to indicate the encoding type of the file. It is also called magic numbers which are used for the start at text streaming. So downloading CSV means we are streaming text data, so while streaming data these 3 above-mentioned characters are added in streaming for understanding UTF-8 encoding. By default Active Admin streams the CSV response to your browser as it’s generated. This is actually a good thing to prevent request timeout kind of exceptions.

In the test case, as we are receiving a response from the controller. In order to capture CSV part from the response we need to get rid of BOM characters. To do that, we will use the String class’s delete_prefix method.

3. Check CSV data

it 'get expected data in csv' do

  test_user= create(:user)
  get :index, format: :csv
  csv_first_row = CSV.parse(response.body)[1]
  
  expected_first_row = [test_user.first_name, test_user.last_name,              
    test_user.email, test_user.role_name]

  expect(expected_first_row).to match_array(csv_first_row)

end

In the above test scenario, we are testing CSV data. So while testing CSV data, first we need to make one user with the help of a factory_bot_rails gem. So after generating user data then we are requesting for CSV and we are expecting that test_user data should get in CSV.

So this is how I have written Rspec test cases for testing my custom CSV export feature in Active Admin. In a similar way, one can test JSON or XML exports too.

Conclusion:-

Basically, Active admin is a very good gem to develop a basic admin interface. As many of the things are provided inbuilt by this gem. According to business requirements, one needs to decide which features to be tested with custom test cases.

Thanks for reading…

Note: This code snippet has been tested in Rails 5.2.3 app having active_admin gem locked at version 2.6.0. This might or might not work in earlier or newer versions of active_admin gem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.