You need to sign in to do that
Don't have an account?
Ty Whitfield
Need help writing a test for simple VF Page with controller
I have a VF page for the Account page that I needed to create in order to display Contacts that had a specific field check. It simply queries Contacts with that field selected and displays. When I tried to push this into production, I keep getting code coverage errors but I'm not sure how to create a test case for this.
Here is the Apex Code
I tried running it against this test but it fails as well.
Any help will be appreciated.
Here is the Apex Code
public class TrainingSubscriptionListController { @AuraEnabled Public List<Contact> acctContacts {get;set;} public string tmp {get;set;} public void loadPage(){ string AcctId = System.currentPageReference().getParameters().get('Id'); acctContacts = [Select Id, Training_Subscription_User__c, Name, Email from Contact where AccountId= :AcctId and Training_Subscription_User__c = true]; tmp = '<table id="myTable" style="width:100%; table-layout: auto;" cellspacing="0" cellpadding="10"><tr>' + '<td style="padding:15px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' + '<b>Name</b></td>' + // '<td style="width:50px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' + //'</td>' + '<td style=" background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' + '<b>Email</b></td>' + '</tr>'; integer i = 0; while(i < acctContacts.size()) { tmp = tmp + '<tr><td style="padding:15px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Name + '</a></td><td style="padding:5px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Email + '</a></td></tr>'; i++; } tmp = tmp + '</table>'; } }
I tried running it against this test but it fails as well.
@isTest public class TestMultipleAdminCheck { @isTest static void TestContact() { Test.startTest(); Account acct = new Account(Name='Test Account', Training_Subscription_Seats_Available__c = 1, Number_of_Support_Users__c = 1); insert acct; Contact cont1 = new Contact(FirstName='User', LastName='One', AccountId=acct.Id, My_ASCI_Role__c = 'Support User'); Contact cont2 = new Contact(FirstName='User', LastName='Two', AccountId=acct.Id, My_ASCI_Role__c = 'Admin User', Training_Subscription_User__c = true); Contact cont3 = new Contact(FirstName='User', LastName='Three', AccountId=acct.Id, My_ASCI_Role__c = 'General User'); Contact cont4 = new Contact(FirstName='User', LastName='Four', AccountId=acct.Id, My_ASCI_Role__c = 'Support User'); insert cont1; insert cont2; insert cont3; insert cont4; Test.stopTest(); } }
Any help will be appreciated.
All Answers
Looking at what you did, I understand referencing and loading the VF page. I don't understand why you had to create an instance of the controller when the controller is on the page already. If you can explain why, I would appreciate. Again, thanks for your help.
There is no other way to access the controller, ideally it would be done via the PageReference but that cannot be done at the moment.
See PageReference methods here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm