I know similar questions have been asked before, however, I cannot find an example that would lay out how to test my query online.

I am trying to roll out a lightning component that queries a custom object in order to show a table on the dashboard.

This is the class:

public class ActivityController {
  public static List<SCALE_Activity__c> getActivities() {
    return [SELECT Id, Name, Description__c, DueDate__c, Case__r.Name, Client__r.Name, AssignedTo__r.Name, Case__c, Client__c, AssignedTo__c, Type__c, Status__c
    FROM SCALE_Activity__c
    WHERE Status__c != 'Closed'
    ORDER BY DueDate__c ASC];

More info if needed:

The component:

<aura:component controller="ActivityController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global">

    <aura:attribute name="activities" type="List" />
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <lightning:card title="All Due Activities" footer="View All Activities">

  <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
      <tr class="slds-text-heading--label">
          <th scope="col"><div class="slds-truncate" title="ID">ID</div></th>
          <th scope="col"><div class="slds-truncate" title="Due Date">Due Date</div></th>
          <th scope="col"><div class="slds-truncate" title="Reference">Reference</div></th>
          <th scope="col"><div class="slds-truncate" title="Type">Type</div></th>
          <th scope="col"><div class="slds-truncate" title="Description">Description</div></th>
                    <th scope="col"><div class="slds-truncate" title="Status">Status</div></th>

          <th scope="col"><div class="slds-truncate" title="Assigned To">Assigned To</div></th>
      <!-- Use the Apex model and controller to fetch server side data -->
      <aura:iteration items="{!v.activities}" var="result">
            <td><div class="slds-truncate" title="{!result.Name}"><a href="{!'/lightning/r/SCALE_Activity__c/'+ result.Id + '/view'}">{!result.Name}</a></div></td>
          <td><div class="slds-truncate" title="{!result.DueDate__c}">{!result.DueDate__c}</div></td>
          <td><div class="slds-truncate" title="Reference">
            <aura:if isTrue="{!result.Case__r.Name != null}">
                    <a href="{!'/lightning/r/Case__c/'+ result.Case__c + '/view'}">{!result.Case__r.Name }</a>
            <aura:if isTrue="{!result.Client__r.Name != null}">
                    <a href="{!'/lightning/r/Client__c/'+ result.Client__c + '/view'}">{!result.Client__r.Name }</a>
          <td><div class="slds-truncate" title="{!result.Type__c}">{!result.Type__c}</div></td>
          <td><div class="slds-truncate" title="{!result.Description__c}">{!result.Description__c}</div></td>
             <td><div class="slds-truncate" title="{!result.Status__c}">{!result.Status__c}</div></td>
            <td><div class="slds-truncate" title="{!result.AssignedTo__r.Name }"><a href="{!'/lightning/r/User/'+ result.AssignedTo__c + '/view'}">{!result.AssignedTo__r.Name }</a></div></td> 


The Controller:

  doInit: function(component, event, helper) {      
    // Fetch the account list from the Apex controller   


The helper:

  // Fetch the accounts from the Apex controller
  getActivityList: function(component) {
    var action = component.get('c.getActivities');
    // Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
     component.set('v.activities', actionResult.getReturnValue());

Everything is working as it should in the Sandbox, however, I cannot figure out the correct code to test for code coverage and to get above 75% in order to deploy.

I 'adopted and changed' most of the code from Trialhead challenges. I do understand the code, however am stuck with Test the class.

I understand that I have to write a class that creates dummy data, runs the query, and compares the output to the input but am not able to produce that code.

Any help is greatly appreciated!

  • Vert smart post tho. – Arnold Jr. May 4 '18 at 19:37


Create the data.. it's pretty straight forward

Client__c client = new Client__c();
client.Name = 'Client Name';
insert client;

Case__c ca = new Case__c();
ca.Name = 'Case Name';
insert ca;

User u = new User();
u.Name = 'Guessing assigned to is a user';
insert u;

SCALE_Activity__c sa = new SCALE_Activity__c();
sa.Name = 'Scale name';
sa.Description__c = 'Description';
sa.DueDate__c = date.today(); 
sa.Case__c = ca.ID;
sa.Client__c = client.ID;
sa.AssignedTo__c = u.ID;
sa.Type__c = 'Type';
sa.Status__c = 'Status';
insert sa;

Then call your query

List<SCALE_Activity__c> saList = ActivityController.getActivities();

  • Thank you so much!! Some more required fields were needed to fill with your above code (which you could not have known) - but it works now. Got 100% – Henr1k May 5 '18 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.