0

I wrote a Class and a Test class I got the coverage to 60% however I am stuck on getting the rest. The lines that are not passing is that if my query is empty update fields. I am not sure how to test this.

trigger SetAsHomePage on Page__c (after insert, after update) {
    private List<Page__c> tounflag {get;set;}

    if(trigger.new.size() > 1) return;

    for(Page__c p:Trigger.new) {
        if(!p.Home_Page__c) continue;

        tounflag = [select Id, Name from Page__c where Home_Page__c = true and Site_Id__c =:p.Site_Id__c and Id !=:p.Id];

        if(tounflag.isEmpty()) continue;

        for(Page__c pu:tounflag) {
            pu.Home_Page__c = false;
            pu.Site_Id__c = null;
        }       

        update tounflag;
    }

    Public Static testMethod void SetAsNoPagesFound()
    {
        Site site = [select Id from Site limit 1];
        String siteid = site.Id;
        //set up page and template folders
        CMSFolder__c pagefolder = new CMSFolder__c(Site_Id__c = siteid.substring(0,15),Type__c = 'Page', Name='test');
        insert pagefolder;
        CMSFolder__c templatefolder = new CMSFolder__c(Type__c = 'PageTemplate', Name='test');
        insert templatefolder;
        //set up a test template
        PageTemplate__c template = new PageTemplate__c(Name='test', VisualForce_Page_Name__c='TestTemplate', ContentBlockNames__c = 'a,b,c', Folder__c = templatefolder.Id);
        insert template;

        //set up a test page
        Page__c pg = new Page__c(Name='testpage', PageTemplate__c = template.Id, Home_Page__c = true, Folder__c = pagefolder.Id);
        insert pg;

        pg.Home_Page__c = False;
        update pg;
    }
}
1
  • Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests. - How to Write Good Unit Tests – Adrian Larson Jun 6 '18 at 15:06
1

You have a logic error in your trigger; if you decide to bulk upload pages, both marked as the home page for the same site in the same transaction, they'll both be checked in the end.

What you'll want to do is break the checks up as two parts. Part 1 happens before insert/update and verifies each site has only one home page (in this example, earliest page in the transaction "wins", but you could choose to invert the loop if you wanted):

if(Trigger.isBefore) {
  Set<Id> siteIds = new Set<Id>();
  for(Page__c record: Trigger.new) {
    record.Home_Page__c = record.Home_Page__c && !siteIds.add(record.Site_Id__c);
  }
}

We use Boolean combination logic (&&) plus the Set.add's return value to eliminate the if statement completely.

Afterwards, for part 2, you should check the database in the after trigger:

if(Trigger.isAfter) {
  Set<Id> siteIds = new Set<Id>();
  for(Page__c record: Trigger.new) {
    if(record.Home_Page__c) {
      siteIds.add(record.Site_Id__c);
    }
  }
  if(!siteIds.isEmpty()) {
    Page__c[] updates = [SELECT Id FROM Page__c WHERE Site_Id__c IN :siteIds AND Home_Page__c = TRUE AND Id NOT IN :Trigger.new];
    for(Page__c record: updates) {
      record.Home_Page__c = false;
    }
    update updates;
  }
}

Here, we gather all the Site_Id__c values, if we have any, we query the database and update any records we find. Note that we only need two if statements in this construct; it's not necessary to check if a list is empty before you iterate over it, nor is it harmful to use a DML statement on an empty list.

Finally, you should fix your unit test so that you insert a bunch of Page records, then insert a bunch more, and then verify the output, as stated by Adrian.

I'm not going to write it out fully, but here's what that test would look like:

// Test before logic
Page__c[] pages = createTestPages(site.Id, 5);
pages[0].Home_Page__c = pages[2].Home_Page__c = true;
insert pages;
// Verify output
System.assertEquals(1, [SELECT COUNT() FROM Page__c WHERE Home_Page__c = TRUE]);
System.assertEquals(0, [SELECT COUNT() FROM Page__c WHERE Id = :pages[2].Id AND Home_Page__c = TRUE]);

// Test after trigger
pages = createTestPages(site.Id, 5);
pages[0].Home_Page__c = true;
insert pages;
// Verify output
System.assertEquals(1, [SELECT COUNT() FROM Page__c WHERE Home_Page__c = TRUE]);
System.assertEquals(1, [SELECT COUNT() FROM Page__c WHERE Id = :pages[0].Id AND Home_Page__c = TRUE]);
0

You need at least one more test method, where you insert one or more Page__c records as home pages, and then, in a separate DML action, insert another one as a home page to cause the other ones to be unflagged.

1
  • Do you mean where the query doesnt return anything? If so I tried that but it then didnt hit the line of the IF statement. – Brooke Jun 6 '18 at 14:47

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.