+ Start a Discussion
StephenDavidsonStephenDavidson 

Code Coverage inside soql for loop

I have this class:

 

public class MyBestPractices {
 
   public MyBestPractices() {
// Best_Practices__kav
        List<Id> articleIDs = new List<Id>();
        List<String> articleNumbers = new List<String>();  
        system.debug('will this show?');    
        for (Best_Practices__kav a : [SELECT Id, ArticleNumber, KnowledgeArticleId FROM Best_Practices__kav WHERE PublishStatus = 'Online' and language = 'en_US' and IsVisibleINPkb = false limit 100 ]) {
            String Id = KbManagement.PublishingService.editOnlineArticle(a.KnowledgeArticleId, true);
            if (Id == null) System.debug('##### ERROR EDITING');
               articleNumbers.add(a.ArticleNumber);
        }
        List<Best_Practices__kav> articles = new List<Best_Practices__kav>();
        system.debug('will this show2?');
        for (Best_Practices__kav d : [SELECT Id, KnowledgeArticleId FROM Best_Practices__kav WHERE PublishStatus = 'Draft' AND Language = 'en_US' AND ArticleNumber IN :articleNumbers limit 100]) {
            d.IsVisibleInPkb = true;
            articleIDs.add(d.KnowledgeArticleId);
            articles.add(d);
        }
        update articles;
    }
}

 

And am trying to test it by running this test class:

 

@isTest (SeeAllData = true)
private class MyBestPractices_Test {

    static testMethod void myUnitTest()

    MyBestPractices mb = new MyBestPractices();

    }
}

 

Code coverage indicates the test is not dropping into the for loop.

 

 

First, I would like to know why it is not dropping into the for loop.  Anyone?

Then I will hold still while you all whip the crap out of me for this non-best-practice way of doing things.....:-)

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Yes,

 

You are right @isTest (SeeAllData = true) would allow you to access existing data inside your test class.However,this would allow you to access data inside your test class,not inside your class.

 

I mean if there is any SOQL inside your test class which is accesing your org's data,then @isTest (SeeAllData = true) would allow to access.However,this notation is irrelvant in terms of actual Class.It does not afftect the accessibility there.

 

Hope it is clear to you now !!

All Answers

Chamil MadusankaChamil Madusanka

You have to insert Best_Practices__kav records with PublishStatus = 'Online' and language = 'en_US' and IsVisibleINPkb = false , for first for loop

 

You have to insert Best_Practices__kav records with the properties in second query in for loop.

 

Then you can create your class instance.

 

@isTest (SeeAllData = true)
private class MyBestPractices_Test {

    static testMethod void myUnitTest()
 //insert Best_Practices__kav with particular properties here
    MyBestPractices mb = new MyBestPractices();

    }
}

 Hit the Kudos button if any post helps you - Mark the answer as solution, It might help others running to into similar problem in future.

StephenDavidsonStephenDavidson

My understanding from reading about this was that the

@isTest (SeeAllData = true)

would allow my test to see existing data, thus creating new data should not be necessary.

Vinit_KumarVinit_Kumar

Yes,

 

You are right @isTest (SeeAllData = true) would allow you to access existing data inside your test class.However,this would allow you to access data inside your test class,not inside your class.

 

I mean if there is any SOQL inside your test class which is accesing your org's data,then @isTest (SeeAllData = true) would allow to access.However,this notation is irrelvant in terms of actual Class.It does not afftect the accessibility there.

 

Hope it is clear to you now !!

This was selected as the best answer
StephenDavidsonStephenDavidson

AAHHHHHHHH. 

:-)