1

I'm working on my lightning component page which uses a controller class and I have these lines of code in the controller class:

@AuraEnabled
public static List<SelectOption> typePicklistValue(){...}

which is a picklist dropdown in my lightning component.

Is it possible to assign a value on it in my test class? I tried doing this in the test class:

List<lightningComponentController.SelectOption> result = lightningComponentController.typePicklistValue(SelectOption:[label=Global, selected=true, value=Global]);

but it doesnt work. I'm just trying it anyway but is there a way to put a value on it? is it even possible?

2
  • Are you trying to create and assign specific values to the List<SelectOption> when the typePicklistValue() is executed from a Test Class?
    – Jigar Shah
    Commented Aug 10, 2018 at 2:23
  • yes @JigarShah can I do that?
    – Mae
    Commented Aug 10, 2018 at 2:29

2 Answers 2

0

You can update your typePicklistValue() method as follows. Use the Test.isRunningTest() method to include a branching logic, which verifies if the execution is invoked due to a test code execution and then populate the appropriate values as shown below. Refer the following article on Using SelectOption Class within Apex to understand further.

@AuraEnabled
public static List<SelectOption> typePicklistValue(){

    List<SelectOption> picklistOptions = new List<SelectOption>();

    //Test class specific branching logic to populate specific values in the context of a test code execution
    if(Test.isRunningTest()){

        picklistOptions.add(new SelectOption('value', 'label'));

    }
    else{

        //Your regular logic to populate the SelectOptions
        ..
    }
}

This can be invoked explicitly from your test code as follows.

@isTest
public class YourTestClass{

    static testMethod void unitTest1(){

        .
        .
        //Assuming your class name is CustomClassName
        CustomClassName.typePicklistValue();
    }
}
0
 public class SampleController{
   @AuraEnabled
   public static List<SelectOption> samplePicklist(){
    ... your code
   }
 }

My test class:

static testMethod void testSamplePicklist()
{
    List<SelectOption> selectOpt = SampleController.samplePicklist()[0];

    System.assertEquals('PicklistValue', selectOpt[0]);
}
1
  • 'Hi @Mae' or any other for of greeting should be avoided when answering. Additionally, please review How to Answer, and make sure you provide an explanation, since this seems to be arepetitive pattern in your recent answers, code dumps are not really beneficial since the OP might not necessarily learn and may begin to expect this forum as being a free coding service. Thanks.
    – glls
    Commented Aug 10, 2018 at 2:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .