1

I am trying to write a test class. I am clueless but with help from a buddy I have 32% so far.

The following code snippet is red b/c I don't have coverage for it yet.

What is SF expecting me to write in my test class to get the code covered? Also, what am I supposed to assert?

The code checks field in a given record and if it is blank, remove all records in the related list that had an id in that field (it was a string with record id's)

Thanks

This is the code in the actual class

if ( currentQ.Selected_Contacts_Ids_List__c == Null || currentQ.Selected_Contacts_Ids_List__c == '')
{
    List <training__c> rd = [Select ID FROM training__c WHERE Training_Resource__c = :currentQ.Id];
    delete rd;
    return null;
}

This is a snippet in the test class that is relevant to this. Where to set currentQ.Selected_Contacts_Ids_List__c to Null or '' so it will get to the code that is not covered?

@isTest
public class test_Training_Students 
{
    @testSetup static void test_selectStudents()
    {

        Account account = new Account();
        account.Name='Jas Training Ctr';
        insert account;

        Contact contactInterviewee = new Contact();
        contactInterviewee.FirstName = 'test747';
        contactInterviewee.LastName = 'test747';
        contactInterviewee.Accountid = account.id;
        contactInterviewee.RecordTypeid='012G0000001IiGfIAK';
        contactInterviewee.Active_Candidate__c=true;
        insert contactInterviewee;

        Contact contactInterviewer = new Contact();
        contactInterviewer.FirstName = 'test2';
        contactInterviewer.LastName = 'test2';
        //contactInterviewer.Account = account;
        contactInterviewer.Accountid = account.id;
        insert contactInterviewer;

        Additional_Contact_Role__c acr =  New Additional_Contact_Role__c();
        acr.Name='TestACR';
        acr.recordtypeid = '012G0000000nFlf';
        insert acr;    

        List <Training_Resource__c> trL = New List<Training_Resource__c>();

        Integer x;
        for (x=1;x<3;x++)
        {    
            Training_Resource__c tr = New Training_Resource__c();
            tr.Name = 'Test'+x;
            tr.Selected_Contacts_Ids_List__c = contactInterviewer.id + ','+ contactInterviewer.id; 
            RecordTypeId
            Id 
            Coordinator_Name__c
            Active__c 


            trL.add(tr);

        }
        insert trL;

        for (x=0;x<2;x++)
        {
            Training__c trn = New Training__c();
            trn.Name = 'A'+1;
            trn.Sch__c = contactInterviewee.Id;
            trn.Training_Resource__c= trL[x].id;
            insert trn;
        }        

    } 
          static testMethod void test_Training_Students()
          {
              Test.setCurrentPageReference(new PageReference('Page.vf_Training_Students'));
              cls_Training_Students controller;

              LIST <Training_Resource__c> trList = [SELECT ID From Training_Resource__c LIMIT 1];

              Apexpages.StandardController sc = New apexpages.StandardController(trList[0]);   

              Test.startTest();
              controller = new cls_Training_Students(sc);
              controller.clearAllContacts();
              controller.saveAllContacts();
              controller.updateContactSelection();
              controller.addTraining();
              controller.init();
              String currentContacts_CSV = trList[0].Id;

                List <Training_Resource__c> trL = New List<Training_Resource__c>();

                Integer x;
                for (x=1;x<3;x++)
                {    
                    Training_Resource__c currentQ = New Training_Resource__c();
                    currentQ.Name = 'Test'+x;
                    currentQ.Selected_Contacts_Ids_List__c = '';
                    trL.add(currentQ);

                }


             Test.stopTest();

          }

    }





        }

3 Answers 3

1

You need to either ensure in one of your tests that:

currentQ.Selected_Contacts_Ids_List__c == Null

||  

currentQ.Selected_Contacts_Ids_List__c == ''

This would then cause the test method to enter this if block and you will receive test coverage for the lines in the block.

This is all you need for the coverage, but to write a good test you should assert that the list of training__c records in the if statement is actually deleted.

6
  • As I ask below, where does one assign the field to Null? See errors I get below
    – Apex N-u-b
    Jun 2, 2016 at 11:42
  • Only use one equals = not two == to set a value so Selected_Contacts_Ids_List__c = null Jun 2, 2016 at 12:10
  • I updated the code in my question because I do not know exactly where to set .Selected_Contacts_Ids_List__c to null. Can you tell me where?
    – Apex N-u-b
    Jun 2, 2016 at 15:39
  • Please can you post the full test method? Jun 2, 2016 at 15:41
  • Create a new test method and ensure it has the following code Training_Resource__c tr = New Training_Resource__c(); tr.Name = 'Test'+x; tr.Selected_Contacts_Ids_List__c = null; Jun 3, 2016 at 9:22
0

Looks like Selected_Contacts_Ids_List__c field is not null, hence it is not entering the if block. First make that field as null in your test class so that it enters the If block. Once it enters the if block, it will query with the record id and delete them. Again run the same query (Select ID FROM training__c WHERE Training_Resource__c = :currentQ.Id) and assert if the size of the list is 0. When the If block gets executed, it will delete the records from the query so your System.assertEquals(0, rd.size()) should be true.

2
  • Where in the testclass do I assign it as Null? Whenever I try it I get an error. If I try something like Selected_Contacts_Ids_List__c == Null in the testsetup I get Expression cannot be a statement error. If I put it after test.starttest() I get the error Variable does not exist: Selected_Contacts_Ids_List__c
    – Apex N-u-b
    Jun 2, 2016 at 11:39
  • You are not assigning the value as null instead you are comparing which is wrong. Try doing Selected_Contacts_Ids_List__c = null instead of Selected_Contacts_Ids_List__c == Null. I am sure it will work.
    – Captain
    Jun 2, 2016 at 19:32
0

After defining currentQ, you have to make that field either null or blank by: currentQ.Selected_Contacts_Ids_List__c = Null; (ONE equal sign)

or

currentQ.Selected_Contacts_Ids_List__c = '';

Then your code will go to the condition. You can assert by: system.assertequals([methodName],null);

You must log in to answer this question.

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