+ Start a Discussion
Brian RomanowskiBrian Romanowski 

System.Assert failure: Working manually, but test method fails....

I have part of a trigger setting the owner of a record when they edit it. I've tested it manually and it's working but my assertion in my test is failing and I can't figure out why. What am I missing??

Trigger:
if (Trigger.isBefore){           
		if (Trigger.isInsert){     
                        // LOGIC TO BE EXECUTED FOR BEFORE INSERT
		}
        //END BEFORE INSERT
		if (Trigger.isupdate){   
                        // LOGIC TO BE EXECUTED FOR BEFORE UPDATE
			//*******************************************************************
            //***********INCIDENT OWNER SET TO STAFF MEMBER WHEN THEY EDIT**************
            //*******************************************************************
            // Create a set of staff ID's
			Set<ID> staffID = new Set<ID>();
			FOR (User s : [select Id from User WHERE BMCServiceDesk__IsStaffUser__c = true AND isactive = True ]) {
    			staffID.add(s.ID);
   			}
	
    		//check if user editing incident is staff
			if(staffID.contains(Userinfo.getUserId())){
			//iterate through the qualifying incidents and set the owner to the staff member editing it   
				FOR (BMCServiceDesk__Incident__c i : Trigger.new){       
					//If they are not changing the owner to someone else set the owner to the current staff user
                    if(Trigger.oldMap.get(i.Id).OwnerID == i.OwnerId){        
						i.OwnerId=Userinfo.getUserID();
        			} 
        		}
    		} 
		}
        //END BEFORE UPDATE
	}
My Test:
@IsTest
public class testmethodtests {
public static testMethod void UpdatedByStaffAssignment() { 
   
    	BMCServiceDesk__Category__c cat = new BMCServiceDesk__Category__c ( Name = 'TestCat',BMCServiceDesk__AvailableForIncidents__c=TRUE);
    		insert cat;
		
        BMCServiceDesk__Status__c teststat = new BMCServiceDesk__Status__c ( Name = 'Teststat');
			insert teststat;    
        
    	User usr = [select Id from User where BMCServiceDesk__IsStaffUser__c = FALSE AND isactive = True LIMIT 1];
        
		User staff = [select Id from User where BMCServiceDesk__IsStaffUser__c = TRUE AND isactive = True  LIMIT 1];

    	User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        
  		BMCServiceDesk__Incident__c inc = new BMCServiceDesk__Incident__c(Ownerid = usr.id,Subject__c='Testy', BMCServiceDesk__FKCategory__c = cat.Id,Status_Map__c='Opened');
			insert inc;	
    	
					
    	test.startTest(); 
            System.runAs(staff) {
    		inc.Status_Map__c = 'Teststat';
    		update inc;
            }
    	test.stopTest();
        
    	BMCServiceDesk__Incident__c updatedInc = [SELECT Id , ownerid FROM BMCServiceDesk__Incident__c WHERE Id = :inc.id];
    	system.debug('usr id'+usr.id);
        system.debug('stf id'+staff.id);
        system.debug('ownr id'+updatedInc.ownerid);
        system.assertEquals(staff.id, updatedInc.ownerid);  
	}


Brian RomanowskiBrian Romanowski
I updated my test to pull out some unnecessary things and commented it out.
@IsTest
public class testmethodtests {
public static testMethod void UpdatedByStaffAssignment() { 
   
    	//Required field
    	BMCServiceDesk__Category__c cat = new BMCServiceDesk__Category__c ( Name = 'TestCat',BMCServiceDesk__AvailableForIncidents__c=TRUE);
    		insert cat;
		        
    	//User to create with
    	User usr = [select Id from User where BMCServiceDesk__IsStaffUser__c = FALSE AND isactive = True LIMIT 1];
        
		//User it should change owner to
    	User staff = [select Id from User where BMCServiceDesk__IsStaffUser__c = TRUE AND isactive = True  LIMIT 1];
        
  		//Create incident with usr as owner
    	BMCServiceDesk__Incident__c inc = new BMCServiceDesk__Incident__c(Ownerid = usr.id, BMCServiceDesk__FKCategory__c = cat.Id);
			insert inc;	
    					
    	//update with staff user
    	test.startTest(); 
            System.runAs(staff) {
    		inc.subject__c = 'Teststat';
    		update inc;
            }
    	test.stopTest();
        
    	//Query updated incident and very that staff is now the owner
    	BMCServiceDesk__Incident__c updatedInc = [SELECT Id , ownerid FROM BMCServiceDesk__Incident__c WHERE Id = :inc.id];
    	system.debug('usr id'+usr.id);
        system.debug('stf id'+staff.id);
        system.debug('ownr id'+updatedInc.ownerid);
        system.assertEquals(staff.id, updatedInc.ownerid);  
	}
}


Brian RomanowskiBrian Romanowski
Figured it out. Had to chenge the run as user for the insert and change it for the update.
//update with staff user
    	test.startTest(); 
        System.runAs(usr) {
           insert inc;	
        }    
        System.runAs(staff) {
    		inc.subject__c = 'Teststat';
    		update inc;
        }
    	test.stopTest();