2

I don't understand why the lines of my trigger marked with

Blockquote

aren't covered. Please can you take a look?

TRIGGER:

trigger SetAllowPortalRegistration on Opportunity (before insert,before update) {

 Map<String, List<Opportunity>>  opportunitiesToUpdate = new Map<String, List<Opportunity>>();
 List<Contact> ContactsToUpdate=new List<Contact>();
        // ONLY IF THE FIELD IS CHANGED SLA_Expiry__c ??
        for (Opportunity opp : Trigger.new) 
        {
             if (opp.SLA_Expiry__c > date.today()){
                   if ((opportunitiesToUpdate.get(opp.AccountId) == null))
                    opportunitiesToUpdate.put(opp.AccountId, new List<Opportunity>());

                    opportunitiesToUpdate.get(opp.AccountId).add(opp);              
              }              
         }



            for(Contact co : [select firstname,lastname,CanAllowPortalSelfReg,ActiveContact__c From Contact WHERE accountid IN :opportunitiesToUpdate.keySet()])
                {
                          if (co.ActiveContact__c=='Yes'&& co.CanAllowPortalSelfReg!=true){
                                   co.CanAllowPortalSelfReg =true;
                                   ContactsToUpdate.add(co); 
                           }
                }
                if (ContactsToUpdate.size()>0)

update(ContactsToUpdate);

}

OLD TEST CLASS: @isTest private class AllowPortalRegistrationTest{

    static testMethod void AllowPortalRegistrationTest() {

    Account acct = new Account(name='test account');
    insert acct;
    Account acct2 = new Account(name='test');
    insert acct2;



    Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Qualified',CloseDate=Date.today()+2, AccountId = acct2.id,LeadSource='Purchased database',MntRenewalDate__c = system.TODAY()+2);
    insert opp1;


    Opportunity opp2 = new Opportunity (Name='Opp2',StageName='Qualified',CloseDate=Date.today()+2, AccountId = acct.id, MntRenewalDate__c = system.TODAY()+3,LeadSource='Purchased database');
    insert opp2;


    Contact[] contactsToCreate = new Contact[]{};

    for(Integer x=0; x<200;x++){
        Contact ct = new Contact(AccountId=acct.Id,lastname='test',email='[email protected]',ActiveContact__c='Yes',CanAllowPortalSelfReg=false);
        contactsToCreate.add(ct);

    }



     Contact[] contactsToCreatenoOpp = new Contact[]{};

    for(Integer x=0; x<200;x++){
        Contact ct = new Contact(AccountId=acct2.Id,lastname='test',email='[email protected]',ActiveContact__c='Yes',CanAllowPortalSelfReg=false);
        contactsToCreate.add(ct);

    }

        Test.startTest();
         insert contactsToCreate;
         insert contactsToCreatenoOpp;     
         update contactsToCreatenoOpp;

        Test.stopTest();
    }

}

LATEST TEST CLASS:

@isTest
private class SetAllowPortalRegistrationTest{

        static testMethod void SetAllowPortalRegistrationTest() {



        Account acct = new Account(name='test account');
        insert acct;

        Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Qualified', AccountId = acct.id,LeadSource='Purchased database',closedate=system.today(),MntRenewalDate__c =system.TODAY().addDays(4));
        insert opp1;



        Opportunity opp2 = new Opportunity (Name='Opp2',StageName='Qualified', AccountId = acct.id,LeadSource='Purchased database',MntRenewalDate__c = system.TODAY().addDays(4),closedate=system.today());
        insert opp2;


        Contact[] contactsToCreate = new Contact[]{};

        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(AccountId=acct.Id,lastname='test',email='[email protected]',ActiveContact__c='Yes',CanAllowPortalSelfReg=false);
            contactsToCreate.add(ct);

        }


             Test.startTest();  

             insert contactsToCreate;

             Opp1.name='newname';
             update Opp1;

            Test.stopTest();
        }

}

With the latest test class I get just also the row if (co.ActiveContact__c=='Yes'&& co.CanAllowPortalSelfReg!=true)covered by the test.

How can i cover the remanent line?

Thanks in advantage for any advice.

BR.

2
  • Did you try setting the SLA_Expiry__c field in your test?
    – Adrian Larson
    Aug 20, 2013 at 20:47
  • Thanks a lot Adrian but the field SLA_Expiry__c it's a formula field.I'm not allowed to write a value in it. I'm setting in test opportunities the field MntRenewalDate__c.The formula field creates just a copy of this field Mnt.
    – Enry
    Aug 20, 2013 at 20:53

2 Answers 2

5

The problem here is that your trigger code is a trigger on Opportunities. It fires when you insert or update an opportunity.

In your test, you insert both your Opportunities before you have created any Contacts. So when the trigger runs, there are no Contacts for it to find.

At the block of code between Test.StartTest() and Test.stopTest() all you are doing is inserting and updating Contacts which doesn't fire the Opportunity trigger at all.

You probably should amend the code so that you do an update to Opportunity after the contacts have been set up, to test this part of the trigger. Unless you are expecting it to work when Contacts themselves are changed in which case you need a trigger directly on Contact.

1
  • Thanks to all!! I have tried so but now i get just also the row if (co.ActiveContact__c=='Yes'&& co.CanAllowPortalSelfReg!=true)covered by the test. I have inserted my new test class in the question.Please could you help me again?.It's very strange :(
    – Enry
    Aug 21, 2013 at 19:55
1

Your SOQL query getting the list of contacts isn't returning any results. I think this is because the key for your map is a string. Try making the map key an Id instead.

3
  • Thanks Daniel, I 've tried but nothing. I have tried also to remove WHERE accountid IN :opportunitiesToUpdate.keySet().Looks like there aren't records.I don't understand.
    – Enry
    Aug 21, 2013 at 7:16
  • 1
    Looking at your test code again, I agree with Doug B. Aug 21, 2013 at 14:36
  • 1
    Thanks,could you take a look again to the question?.It's updated with the new test.I still have some problem.
    – Enry
    Aug 21, 2013 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .