You need to sign in to do that
Don't have an account?

What do I need to add to my test class to get it to cover the rest of my trigger?
I can't seem to get my test code to check a chunk of my trigger. I have a trigger, a class to prevent recursion, and my test class.
Trigger (basically any time the Opp Stage changes a pair or records are created in a custom object assigning points to the move to and from the stage.) Lines 15 through 30 are not covered by the test class listed at the bottom.
line source 1 trigger UpdateSalesScoreEvents on Opportunity (After update) { 2 3 try { 4 5 // List of variables to be used later. 6 List<Sales_Score__c> ScoreEvents = new list<Sales_Score__c>(); 7 List<Opportunity> OldStage = New list<Opportunity>(); 8 9 10 // Add to the list all opps associated with an agreement in the trigger that has been signed 11 for (Opportunity Opp: Trigger.New) { 12 if ((Opp.Company_Division__c == 'Textura') && (Opp.StageName != trigger.oldMap.get(Opp.id).StageName) && (!ScoreEventHelper.hasAlreadyCreatedScores())){ 13 14 // 15 Sales_Score__c OldEvent = new Sales_Score__c(); 16 OldEvent.Opportunity__c = opp.id; 17 OldEvent.Direction__c = 'Leaving Stage'; 18 OldEvent.Movement_Date__c = system.Today(); 19 OldEvent.Stage__c = Trigger.oldMap.get(opp.ID).StageName; 20 // Add to ScoreEvent list 21 ScoreEvents.add(OldEvent); 22 23 // 24 Sales_Score__c Event = new Sales_Score__c(); 25 Event.Opportunity__c = opp.id; 26 Event.Direction__c = 'Entering Stage'; 27 Event.Movement_Date__c = system.Today(); 28 Event.Stage__c = opp.StageName; 29 // Add to ScoreEvent list 30 ScoreEvents.add(Event); 31 32 33 } 34 ScoreEventHelper.setAlreadyCreatedScores(); 35 insert ScoreEvents; 36 } 37 } catch (Exception e) {Trigger.new[0].addError(e.getMessage());} 38 }
Class used to prevent recurrsion;
1 public class ScoreEventHelper{ 2 3 // Static variables are local to the context of a Web request 4 5 // (or testMethod during a runTests call) 6 7 // Therefore, this variable will be initialized as false 8 9 // at the beginning of each Web request which accesses it. 10 11 12 private static boolean alreadyCreatedScores = false; 13 14 15 public static boolean hasAlreadyCreatedScores() { 16 return alreadyCreatedScores; 17 } 18 19 // By setting the variable to true, it maintains this 20 21 // new value throughout the duration of the request 22 23 // (or testMethod) 24 25 public static void setAlreadyCreatedScores() { 26 alreadyCreatedScores = true; 27 } 28 }
This is the test method I created. Something in it needs to be added to check more code from the trigger.
1 public class ScoreEventTests { 2 static testMethod void TestScoreEvents() { 3 User user1 = new User(); 4 user1.FirstName = 'Steve'; 5 user1.LastName = 'Will'; 6 user1.UserName = 'stevew@submittalexchange.com'; 7 user1.Email = 'stevew@submittalexchange.com'; 8 user1.Alias = 'swill'; 9 user1.CommunityNickname = 'steve'; 10 user1.TimeZoneSidKey = 'America/Chicago'; 11 user1.LocaleSidKey = 'en_US'; 12 user1.LanguageLocaleKey = 'en_US'; 13 user1.EmailEncodingKey = 'UTF-8'; 14 user1.ProfileId = '00e70000000swZU'; 15 insert user1; 16 17 Account account = new Account(); 18 account.Name = 'Test GradeBeam Account'; 19 account.Type = 'Architect'; 20 account.AD_Rep__c = user1.Id; 21 account.NCA_Rep__c = user1.Id; 22 account.Account_Source__c = 'BSD'; 23 insert account; 24 25 Opportunity opp1 = new Opportunity(); 26 opp1.Name = 'Test Opp'; 27 opp1.AccountId = account.id; 28 opp1.CloseDate = System.today(); 29 opp1.StageName = 'Qualified'; 30 opp1.Amount = 100; 31 opp1.OwnerId = user1.Id; 32 opp1.Company_Division__c = 'Textura'; 33 insert opp1; 34 35 36 opp1.StageName = 'Sales Process'; 37 update Opp1; 38 39 opp1.StageName = 'Written Agreement'; 40 update Opp1; 41 42 opp1.StageName = 'Negotiation'; 43 update Opp1; 44 } 45 }
You need to test that the event data was properly created. Something like this...
Let me know if you have any questions.
All Answers
The test class is looking good.Can you let me know which lines are not getting covered?
Lines 15 through 30. Thats the part that actually creates and inserts these two records.
You need to test that the event data was properly created. Something like this...
Let me know if you have any questions.
These should be covered.Can you check in debug logs that the opportunity is getting inserted.Also,make sure that there is no Validation rules on the object which is being inserted in Trigger i.e. Sales_Score__c and if there is your are respecting it.
Well this leads me to ask where in that massive debug log can I easily pinpoint if a test class inserted the records successfully?
You can test whether a record was creatd with asserts...
Put a debug statement in the Testclass just after you have inserted the Opportunity record.
Like below
system.debug('##########' + opp1);
Then,search for #### in the debug log,there you can see if the record was inserted or not.