You need to sign in to do that
Don't have an account?
Phuc Nguyen 18
Apex Test Class for AuraEnabled
Having trouble determing how to write a Test class for my Lightning Controller.
public with sharing class NewAcctCustController { @AuraEnabled public static List<String> getStates() { List<String> stateList = new List<String> {'AL', 'AK', 'AZ', 'AR', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'}; return stateList; } @AuraEnabled public static List<String> getPicklistOptionsforCust() { return getPicklistValues('Customer__c', 'Cust_Type__c'); } public static List<String> getPicklistValues(String objectAPI, String fieldAPI) { List<String> picklistValues = new List<String>(); List<Schema.PicklistEntry> values = Schema.getGlobalDescribe().get(objectAPI).getDescribe().fields.getMap().get(fieldAPI).getDescribe().getPicklistValues(); for (Schema.PicklistEntry val : values) { picklistValues.add(val.getLabel()); } return picklistValues; } @AuraEnabled public static string submitObjecttoApex(sObject acct, sObject cust,sObject proj) { try { Account__c oacct = (Account__c)acct; Customer__c ocust = (Customer__c)cust; Customer_Project oproj = (Customer_Project__c)proj; oacct.Project_Type__c = 'New Customer'; insert acct; Decimal custnum = oacct.Customer_num__c; List<acct__Project_Template__c> template = [SELECT Id FROM Project_Template__c WHERE Name = 'New Potential Customer' LIMIT 1]; if (template != null && template.size() > 0) { ocust.ProjectTemplate__c = template[0].Id; } ocust.Account__c = oacct.Id; insert cust; oproj.Customer__c = ocust.Id; Customer_Project existingcustproj = [select id from Customer_Project__c where Project__c =: oproj.Id limit 1]; existingcustproj.CompType= oproj.CompType__c; existingcustproj.Type= oproj.CustProj__c; existingcustproj.Apptype= oproj.App_Type__c; update existingcustproj; } } }
There's nothing special about testing methods with @AuraEnabled or any other modifier. Create a "@IsTest" class, then an "@IsTest" method, and call your methods just as you would non-aura code. Your submitObjecttoApex method is a little complex, but still easily testable.
Checkout some of my blog posts (http://force-code.com/unit-testing-tips-tricks-part-1/) at Force-Code.com and my book, Salesforce Defect Domination (http://force-code.com/salesforce-defect-domination-a-handbook-for-finding-fixing-and-preventing-defects-in-force-com-development/), to get ideas on how to improve the testability of your methods.
Best,
~Steve