0

Im stuck at 52% coverage of a vf controller that queries and displays timecards__c records of UserInfo.getUserId in a table. What is basically not getting covered is the if else statements after the query in fetchtimecards() and several parts of that method. Appreciate the help anyone.

if((relatedAssignment.RecordType.Name == 'Primary' && relatedAssignment.Resource__r.User__c == UserInfo.getUserId()) ||
                (relatedAssignment.RecordType.Name == 'Shadow' && relatedAssignment.EP_Shadow_Resource__r.User__c == UserInfo.getUserId()) &&
                (relatedAssignment.Time_Cards__r.size() > 0 && relatedAssignment.Time_Cards__r != null )) {

Controller

/**
*  @Class Name: PendingTimeCardsPageController
*  @Description: Handles all functionality required for "PendingTimeCardsPage" visualforce page.
*/
public class PendingTimeCardsPageController {

    public String currentResourceName {
        get;
        set;
    }
    public List < timeCardWrapper > timeCardWrapperList {
        get {
            if (timeCardWrapperList != null) {
                return timeCardWrapperList;
            }
            return fetchTimeCards();
        }
        public set;
    }
    public String errorMessage{get;set;}
    
    public List < timeCardWrapper > fetchTimeCards() {
        
        timeCardWrapperList = new List < timeCardWrapper > ();
        currentResourceName = UserInfo.getName();
        
        for (Assignment__c relatedAssignment: [SELECT Id, Name, RecordTypeId, Billing_Rate__c, Approval_Type__c, 
            Start_Date__c, End_Date__c,
                Billing_Type__c, EP_Shadow_Resource__c, Is_Primary_Resource_Contributing__c, RecordType.Name, 
                Resource__r.User__c, EP_Shadow_Resource__r.User__c ,
                Resource_Name__c, Role__c, Resource__c, Status__c, Assignment_Type__c, Project__c, Project__r.Name, 
                EP_Shadow_Resource__r.User__r.Name, Resource__r.User__r.Name,
                (SELECT Id, Name, Assignment__r.EP_Shadow_Resource__c, Assignment__r.Resource__c,
                    Type__c, EP_End_Date__c, Billable_Hours_Fri__c, EP_Notes__c, EP_Approval_Date__c,
                    EP_Total_Number_of_Billable_Hours__c, EP_Total_Number_of_Unbillable_Hours__c,
                    EP_Rejected_Date__c, Submitted_On__c, Project_Manager__c, Billable_Hours_Mon__c,
                    Billable_Hours_Sat__c, Assignment__c, Status__c, Start_Date__c, Billable_Hours_Sun__c,
                    Billable_Hours_Thu__c, Billable_Hours_Tue__c, Billable_Hours_Wed__c, UnBillable_Hours_Fri__c,
                    UnBillable_Hours_Mon__c, UnBillable_Hours_Sat__c, UnBillable_Hours_Sun__c, UnBillable_Hours_Thu__c, 
                    Assignment__r.RecordType.Name,
                    UnBillable_Hours_Tue__c, UnBillable_Hours_Wed__c, Assignment__r.Project__c, Assignment__r.Project__r.Name,
                    X1st_Approver__c, X2nd_Approver__c, X3rd_Approver__c, X4th_Approver__c, X5th_Approver__c, 
                    Assignment__r.Is_Primary_Resource_Contributing__c,
                    Assignment__r.Resource__r.User__c
                    FROM Time_Cards__r 
                    WHERE Status__c != 'Approved' AND Status__c != 'Client Approved'
                    ORDER BY Start_Date__c Desc)
                FROM Assignment__c
                WHERE EP_Shadow_Resource__r.User__c =: UserInfo.getUserId() OR Resource__r.User__c =: UserInfo.getUserId()]) {
            if((relatedAssignment.RecordType.Name == 'Primary' && relatedAssignment.Resource__r.User__c == UserInfo.getUserId()) ||
                (relatedAssignment.RecordType.Name == 'Shadow' && relatedAssignment.EP_Shadow_Resource__r.User__c == UserInfo.getUserId()) &&
                (relatedAssignment.Time_Cards__r.size() > 0 && relatedAssignment.Time_Cards__r != null )) {
                
                timeCardWrapper newTimeCardWrpper = new timeCardWrapper();
                newTimeCardWrpper.relatedAssignment = relatedAssignment;
                for (Time_Card__c relatedTimeCard: relatedAssignment.Time_Cards__r) {
                    if (relatedTimeCard.Status__c != 'Submitted') {
                        if (newTimeCardWrpper.assignmentWithTimeCardsMap.get(relatedTimeCard.Assignment__c) != null &&
                            !newTimeCardWrpper.assignmentWithTimeCardsMap.get(relatedTimeCard.Assignment__c).contains(relatedTimeCard)) {
                            newTimeCardWrpper.assignmentWithTimeCardsMap.get(relatedTimeCard.Assignment__c).add(relatedTimeCard);
                        } else {
                            List < Time_Card__c > releatedTimeCardsList = new List < Time_Card__c > ();
                            releatedTimeCardsList.add(relatedTimeCard);
                            newTimeCardWrpper.assignmentWithTimeCardsMap.put(relatedTimeCard.Assignment__c, releatedTimeCardsList);
                        }
                    }
                }
                if(!newTimeCardWrpper.assignmentWithTimeCardsMap.isEmpty()){
                    timeCardWrapperList.add(newTimeCardWrpper);
                }
            }
        }
        if(timeCardWrapperList.size() == 0 ){
            errorMessage = 'No Pending Timesheets';
        }

        
        return timeCardWrapperList; 
    }

    public class timeCardWrapper {
        
        public Assignment__c relatedAssignment {
            get;
            set;
        }
        public Map < Id, List < Time_Card__c >> assignmentWithTimeCardsMap {
            get;
            set;
        }
        
        public timeCardWrapper() {
            this.relatedAssignment = new Assignment__c();
            this.assignmentWithTimeCardsMap = new Map < Id, List < Time_Card__c >> ();
        }
    }
}

Test Class

@isTest
private class PendingTimeCardsPageControllerTest{
    private static testMethod void test() {

        Profile prof = [select id from profile where name = 'System Administrator'];
            User user = new User();
            user.LastName = 'Test User';
            user.Alias = 'tu';
            user.Email = 'test@gmail.com';
            user.Username = 'test.dev@stand.com';
            user.CommunityNickname = 'test.t';
            user.User_Time_Zone__c = 'Pst';
            user.ProfileId = prof.id;          
            user.TimeZoneSidKey    = 'America/Denver';
            user.LocaleSidKey      = 'en_US';
            user.EmailEncodingKey  = 'UTF-8';
            user.LanguageLocaleKey = 'en_US';
            insert user;
        
            Account acc1 = new Account();
            acc1.Name = 'Test Account1';
            insert acc1;
             
            Contact con1 = new Contact();
            con1.FirstName = 'test1';
            con1.LastName = 'contact1';
            con1.AccountId = acc1.id;
            insert con1;
        
            Profile prof1 = [select id from profile where name = 'Manager Team Profile - Community'];      
            User user1 = new User();
            user1.LastName = 'Test User1';
            user1.Alias = 'tu1';
            user1.Email = 'test1@gmail.com';
            user1.Username = 'test1.dev@stand.com';
            user1.CommunityNickname = 'test1.t';
            user1.ContactId = con1.id;
            user1.User_Time_Zone__c = 'Pst';
            user1.ProfileId = prof1.id;          
            user1.TimeZoneSidKey    = 'America/Denver';
            user1.LocaleSidKey      = 'en_US';
            user1.EmailEncodingKey  = 'UTF-8';
            user1.LanguageLocaleKey = 'en_US';
            insert user1;
            
            Account acc = new Account();
            acc.Name = 'Test Account';
            insert acc;
            
            Contact con = new Contact();
            con.FirstName = 'test';
            con.LastName = 'contact';
            con.AccountId = acc.id;
            insert con;
        
                    Account acc2 = new Account();
            acc2.Name = 'Test Account2';
            insert acc2;
            
            Contact con2 = new Contact();
            con2.FirstName = 'test';
            con2.LastName = 'contact2';
            con2.AccountId = acc2.id;
            insert con2;
        
            Profile prof2 = [select id from profile where name = 'Executive(Community)'];      
            User user2 = new User();
            user2.LastName = 'Test User2';
            user2.Alias = 'tu2';
            user2.Email = 'test2@gmail.com';
            user2.Username = 'test2.dev@stand.com';
            user2.CommunityNickname = 'test2.td';
            user2.ContactId = con2.id;
            user2.User_Time_Zone__c = 'Pst';
            user2.ProfileId = prof2.id;          
            user2.TimeZoneSidKey    = 'America/Denver';
            user2.LocaleSidKey      = 'en_US';
            user2.EmailEncodingKey  = 'UTF-8';
            user2.LanguageLocaleKey = 'en_US';
            insert user2;
        
       /* Contact con3 = new Contact();
            con3.FirstName = 'test3';
            con3.LastName = 'contact3';
            con3.AccountId = acc2.id;
            insert con3;*/
        
           /* User user3 = new User();
            user3.LastName = 'Test User3';
            user3.Alias = 'tu23';
            user3.Email = 'test23@gmail.com';
            user3.Username = 'test23.dev@stand.com';
            user3.CommunityNickname = 'test23.td';
            user3.ContactId = con3.id;
            user3.User_Time_Zone__c = 'Pst';
            user3.ProfileId = prof2.id;          
            user3.TimeZoneSidKey    = 'America/Denver';
            user3.LocaleSidKey      = 'en_US';
            user3.EmailEncodingKey  = 'UTF-8';
            user3.LanguageLocaleKey = 'en_US';
            insert user3;*/
        
            Opportunity opp1 = new Opportunity();
            opp1.AccountId = acc.id;
            opp1.Name = 'TestOpp1';
            opp1.OwnerId = user.id;
            opp1.CloseDate = Date.newInstance(2021, 31, 3);
            opp1.StageName = 'Closed Won';
            opp1.Type = 'New Project';
            insert opp1;
        
        
        
          Project__c project = new Project__c();
            project.Name = 'Test Project';
            project.EP_Project_Manager__c = user1.Id;
            project.Contact_Person__c = con.Id;
            project.Opportunity__c = opp1.id;
            project.Start_Date__c = Date.newInstance(2020, 31, 3);
            project.End_Date__c = Date.newInstance(2020, 31, 12);
            project.Account__c = acc.id;
            project.Number__c = 1;
        project.Contractual_Start_Date__c = Date.newInstance(2020, 1, 1);
        project.Contractual_End_Date__c = Date.newInstance(2020, 31, 12);
            insert project;        
            
        
            Resource__c resource = new Resource__c();
            resource.User__c = user2.Id;
            resource.Contact__c = con2.Id;
            resource.Active__c = true;
            insert resource;   
        
               /*     Resource__c resource1 = new Resource__c();
            resource1.User__c = user3.Id;
            resource1.Contact__c = con3.Id;
            resource1.Active__c = true;
            insert resource1; */
        
            Assignment__c assignment = new Assignment__c();
            assignment.Project__c = project.id;
            assignment.Resource__c = resource.id;
            assignment.Status__c = 'Approved';
           assignment.Start_Date__c = Date.newInstance(2020, 1, 5);
            assignment.End_Date__c = Date.newInstance(2020, 31, 10);
            assignment.Role__c = 'Developer';
            assignment.Billing_Type__c = 'Billable';
            assignment.Assignment_Type__c = 'Long Term';  
            assignment.RecordTypeId =[SELECT Id, Name, DeveloperName, SobjectType FROM RecordType WHERE SobjectType = 'Assignment__c' AND Name ='Primary'].Id;
          assignment.Budgeted_Hours__c= 40;
            insert assignment;
        
                  /*  Assignment__c assignment1 = new Assignment__c();
            assignment1.Project__c = project.id;
            assignment1.Resource__c = resource.id;
            assignment1.EP_Shadow_Resource__c = resource1.id;
            assignment1.Status__c = 'Approved';
           assignment1.Start_Date__c = Date.newInstance(2020, 1, 9);
            assignment1.End_Date__c = Date.newInstance(2020, 12, 9);
            assignment1.Role__c = 'Developer';
            assignment1.Billing_Type__c = 'Billable';
            assignment1.Assignment_Type__c = 'Long Term';  
            assignment1.RecordTypeId =[SELECT Id, Name, DeveloperName, SobjectType FROM RecordType WHERE SobjectType = 'Assignment__c' AND Name ='Shadow'].Id;
          assignment1.Budgeted_Hours__c= 40;
            insert assignment1;*/

    /* List<Time_Card__c> timeCardList = [SELECT Id,Start_Date__c,EP_End_Date__c FROM Time_Card__c WHERE Assignment__C =: assignment.Id Limit 10];
            timeCardList[0].Status__c = 'Assigned';
            timeCardList[1].Status__c = 'Draft';
            update timeCardList;  
       
        Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
          attach.parentId=timeCardList[1].id;
          insert attach; */
       
        system.runas(user2){
            // Test.startTest();
         pagereference pageref1 = page.PendingTimeCardsPage;
           Test.SetCurrentPageReference(pageref1);
        PendingTimeCardsPageController f1 = new PendingTimeCardsPageController();
 
        PendingTimeCardsPageController.timeCardWrapper wrp = new PendingTimeCardsPageController.timeCardWrapper();
        f1.fetchTimeCards();
        f1.currentResourceName =user2.Name;
           // Test.stopTest();
        }        
    }
}
2
  • You have a lot going on here. At a quick glance, this should work. Inserting a user and using RunAs should give you that user id. I'd recommend debugging by commenting/removing all the IF criteria except the user id. I'd also recommend adding a system debug during the user insert and before the IF, so you can see whether the user id is different than expected. But I'd bet something else is going on here to cause the IF to be skipped. – JimG Oct 14 '20 at 12:06
  • Hey JimG, so I figured it out by running two different user.id run as seperately for Primary and Shadow recordtypes while calling the class in the system.runas. It shot to 80%. Appreciate the help man. – Kevin S Oct 20 '20 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.