0

I have been through Trailhead and read multiple references, but am still not getting passing tests. I'm sure I'm missing something simple and need assistance with writing a passing test for a method that returns a boolean value.

Here is the controller extension (method) I need to test.

public with sharing class ShouldShowMedSection{
    public ShouldShowMedSection(ApexPages.StandardController paramController) {
       Intake = paramController;}
    public ApexPages.StandardController Intake {get; set;}
    public Boolean getShouldShowMedSection() {
       Intake__c record = (Intake__c)Intake.getRecord();
        
        
          if(
            record.Personal_Care__c != null && record.Personal_Care__c.contains('Medication')
            ) 
            {
            return true;
            }
      
        
          return false;
        }
        
        }

Here is the test method I need assistance adjusting:

@isTest
public class IntakeExtensionTest {
    
    static testMethod void ShouldShowMedSection() {        
   
        Intake__c i1 = new Intake__c();
        Boolean isValid = false;
        
        Test.StartTest();
        
        i1.Client_Name_First_and_Last_Initial__c = 'Test Client A';
        i1.Personal_Care__c.contains('Medication');
        
        insert i1; 
        
        isValid = Intake__c.validate(i1);
        Test.stopTest();
        System.assert(isValid, true);

    }
}
2

You need to instantiate the controller object first

ShouldShowMedSection ctrl = new ShouldShowMedSection(
     new ApexPages.StandardController(
       new Intake__c(Client_Name_First_and_Last_Initial__c = 'Test Client A',
                     Personal_Care__c = 'xxxMedicationxxx')));

System.assertEquals(true,ctrl. getShouldShowMedSection(),'sb valid');

Notes:

  • The first argument of the assert should be the expected value, not the actual value - in your testmethod, you had them reversed
  • In your controller, this clause record.Personal_Care__c != null can be removed because a standardcontroller will always have a record

The whole controller could be simplified/standardized to typical pattern to:

public with sharing class ShouldShowMedSection{
 ApexPages.StandardController ctrl;   
 public ShouldShowMedSection(ApexPages.StandardController ctrl) {
   this.ctrl = ctrl;
  }
 public Intake__c record {
    get {
     record = this.ctrl.getRecord();
     return record;
     } set; }      

 public Boolean getShouldShowMedSection() {
    return this.record.Personal_Care__c?.contains('Medication');
    }
    
}
  • Perfect! Thank you so muchh – Alexis Frisch Nov 9 '20 at 17:57

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.