function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Padmini Sidugonde 6Padmini Sidugonde 6 

SelectOption code coverage in test class

Hi,

I have written below code in Apex Class. But I am not able to cover this code coverage. Please help on this.
public List<SelectOption> RelatedAccounts
    {
        get{
            system.debug('ACCOUUNTS:' + Accs);
        
            List<SelectOption> options = new List<SelectOption>();
            options.add(new selectOption('--Select--',''));
            
            for(CPF_Relationship__c r : Accs)
            {
                options.add(new SelectOption(r.id,r.child_account__r.name));
            }
            return options;
        }
    }

 
Best Answer chosen by Padmini Sidugonde 6
Biswojeet Ray 11Biswojeet Ray 11

Hi Padmini,

Please try this.

Please put your class in the plase of "YourControllerClassName "
 

YourControllerClassName toGetSelectOption = new YourControllerClassName();
List<SelectOption> selectOptionValuses = toGetSelectOption.RelatedAccounts;

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet

All Answers

Biswojeet Ray 11Biswojeet Ray 11

Hi Padmini,

Please try this.

Please put your class in the plase of "YourControllerClassName "
 

YourControllerClassName toGetSelectOption = new YourControllerClassName();
List<SelectOption> selectOptionValuses = toGetSelectOption.RelatedAccounts;

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet

This was selected as the best answer
Raj VakatiRaj Vakati
You need to do it like this 

Account acc = new Account();
acc.Name ='Test';
insert acc ; 
CPF_Relationship__c tem= new CPF_Relationship__c();
tem.child_account__c =acc.Id ;
insert tem ;
YourControllerClassName toGetSelectOption = new YourControllerClassName();
List<SelectOption> selectOptionValuses = toGetSelectOption.RelatedAccounts;

 
Madhukar_HeptarcMadhukar_Heptarc
Dear Padmini,

I have understand Your requirement.
Syntax : 

ClassName  ReferenceVariable = new ClassName  (new ApexPages.StandardController(new Object_Name())); 

List<SelectOption> aList = ReferenceVariable.RelatedAccounts;


Example :

InsertAccount   Acc= New insertAccount(new ApexPages.StandardController(new Account())); 
List<SelectOption> aList = Acc.RelatedAccounts;

Can you try above code it will resolve your issue.
Please let me know if it's helps you.

Thanks
Madhukar_Heptarc
Padmini Sidugonde 6Padmini Sidugonde 6
Thank you  to all. It works. But it is not covering below one line code. Please suggest on this.

User-added image
 
Tad Aalgaard 3Tad Aalgaard 3
Accs is empty.  You need to find out how Accs is populated.  More than likely you have seeAllData set to false and you will need to create the CPF_Relationship__c records in your Test class.