function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mark GallagherMark Gallagher 

Test Method Not Working

Built a trigger that limits the number of a certain type of Lead a User can have trying to Test it. I have four methods that test four different Users that have four differnt caps 100, 200, 300, and 400. The 200 and the 400 Methods work, but the 100 and 300 don't work. They are all the exact same method and user. I have tried switching the users and that doesn't work, but if I change the caps then it works. The error I'm getting is Assertion Failed

<--- Trigger Code --->
trigger LimitLeads on Lead (after update)
{
    List<Lead> trueLeads = new List<Lead>();
    Set<Id> LeadOwner = new Set<Id>();
    List<User> capName = new List<User>();
    
    for(Integer i = 0; i < Trigger.New.size(); i++)
    {
        if(Trigger.New[i].OwnerId != Trigger.old[i].OwnerId && Trigger.New[i].OwnerId != Null)
        {
            LeadOwner.add(Trigger.New[i].OwnerId);
        }
    }
    
    if(LeadOwner.size() > 0)
    {
        trueLeads = [SELECT Id FROM Lead WHERE (Status = '01-New' OR Status = '20-Nurture') AND
                                               RecordTypeId = '01250000000Mvl2AAC' AND
                                               Lead_Source_Type__c = 'Team Lead' AND
                                               OwnerId in: LeadOwner];
        capName = [SELECT Pipeline_Cap__c, Full_Name__c FROM User WHERE Id in: LeadOwner];
        
        for(Lead ld: Trigger.New)
        {   
            if((ld.Status == '01-New' || ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC' && ld.Lead_Source_Type__c == 'Team Lead')
            {
                Integer pipelineCap = (Integer) capName[0].Pipeline_Cap__c;
                String name = capName[0].Full_Name__c;
                Integer numLeads = trueLeads.size();
                Integer remove = numLeads - pipelineCap;
                if(numLeads > pipelineCap)
                {
                    ld.OwnerId.addError( name + ' too many team leads remove some to add more team leads');
                } 
            } 
        }
    }
}

<--- Method Code --->
   @isTest static void TestCap100True()
    {
        User[] cap100Id =  [SELECT Pipeline_Cap__c FROM User WHERE Full_Name__c = 'Test User']; 
        Id cap100 = cap100Id[0].Id;
        Integer cap = (Integer)cap100Id[0].Pipeline_Cap__c;
        
       
        List<Lead> trueLead = TestLimitLeadsDataFactory.trueLeads();
        
        for(Lead ld: trueLead)
        {
            ld.OwnerId = cap100;
        }
        
        Test.startTest();
        Database.SaveResult[] results = Database.update(trueLead,false);
        Test.stopTest();
        
        for(Integer i = 0; i < results.size(); i++)
        {
            if(i < cap)
            {
                System.assert(results[i].isSuccess());
            }
            else
            {
                System.assert(!results[i].isSuccess());
                System.debug('result ' + i + 'failed');
            }
        }
    }
Raj VakatiRaj Vakati
Can you see is there any assesion errors you are getting .. ? I belive you test method is failing due to assertion issue 
Mark GallagherMark Gallagher
Yes that's what I said it's failing due to a assertion failed
Raj VakatiRaj Vakati
Change an assertion  based on the outcome 
Mark GallagherMark Gallagher
How am I supposed to see whether or not the trigger works properly if I assert what happens. Wouldn't you assert what should happen that way you know whether the trigger is actually working properly?