0

I know that so many questions like that. but no one answer is correct for me. How to cover below lines? (image below) cover that lines

Test

public class TERF_Controller_CreateNew_TEST {
    static testMethod void test() {
        test.startTest();
        TERF_Employee__c employee = new TERF_Employee__c(TERF_EMP_Email__c = UserInfo.getUserEmail(), Name = 'emp', TERF_EMP_FirstName__c = 'first', TERF_EMP_SecondName__c = 'second');
        insert employee;
        TERF_MR__c MR = new TERF_MR__c(TERF_MR_Employee__c = employee.Id);
        Insert MR ;
        System.assert([SELECT Name FROM TERF_MR__c WHERE Id = :MR.Id].Name != null);
        PageReference pageRef = page.TERF_CreateNew;
        pageRef.getParameters().put('id', String.valueOf(MR.Id));
        Test.setCurrentPage(pageRef);
        TERF_Controller_CreateNew contr = new TERF_Controller_CreateNew();
        contr.saveMR();
        contr.gotoRU_TERs_Home();
        test.stopTest();
    }
}

Controller

public class TERF_Controller_CreateNew {
    public TERF_MR__c MR { get; set; }
    public TERF_RI__c report { get; set; }

    public TERF_Controller_CreateNew() {
        MR = new TERF_MR__c(Name = 'New MR', TERF_MR_Status__c = 'Editable');
        report = new TERF_RI__c();
    }

    public pageReference saveMR() {
        try {
            insert MR;
            PageReference pageRef = new PageReference('/apex/TERF_EditMR');
            pageRef.getParameters().put('id', MR.Id);
            return pageRef;
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'You could not delete approved report'));
            PageReference pageRef = ApexPages.currentPage();
            return pageRef;
        }
        return null;
    }

    public PageReference gotoRU_TERs_Home() {

        return new PageReference('/apex/TERF_Home');
    }

}

Validation Rule

saveMR() works only when employee.email = user.email

  • Include test.StartTest() just before you invoke the controller at line TERF_Controller_CreateNew contr = new TERF_Controller_CreateNew(); – Jigar Shah Aug 24 '18 at 10:15
  • Also, please ensure that you take the effort to format the code appropriately, since that enhances readability and facilitates a better understanding of your problem! – Jigar Shah Aug 24 '18 at 10:22
  • the same problem(( – Ivan S. Aug 24 '18 at 10:28
  • it doesn't impact if i change lines – Ivan S. Aug 24 '18 at 10:29
1

You're getting an exception on line 12. Unfortunately, you're not checking the output from your methods, so we don't know what went wrong. You'll want to modify your unit test as follows:

PageReference ref = contr.saveMR();
if(ApexPages.hasMessages()) {
  // show the error message
  System.assert(false, ApexPages.getMessages()[0].getDetail());
}
...

This will at least expose why your MR record failed to save (validation rule, missing required field, etc), and you can make the appropriate adjustments to your unit test from there.

  • yes. i know reason now. validation rule: AND(TERF_MR_Employee__r.TERF_EMP_Email__c <> $User.Email, TERF_MR_Employee__r.TERF_EMP_Assistant__r.TERF_EMP_Email__c <> $User.Email) but it shouldn't called, becouse employee.email = userEmail... – Ivan S. Aug 24 '18 at 10:46
  • it says: YOu can't choose this employee (its error text of validation rule) – Ivan S. Aug 24 '18 at 10:48
  • @YanS no, the validation rule is firing correctly. The MR record in your page is not the same MR record you created in your unit test. TERF_MR_Employee__c is null, so the validation rule is saying it's not allowed. You need to modify the MR record in the controller to link to the correct TERF_Employee__c record. This might actually be a bug in your logic, you might need to adjust the constructor for your controller class. – sfdcfox Aug 24 '18 at 10:54
  • but i can't select from database pageMR because it's not inserted yet.. to bind it with new employee. i know you can find a way, pleaaaase help :) – Ivan S. Aug 24 '18 at 11:11
  • @YanS you have to emulate whatever the user would have to do in the Visualforce page to save the record, including filling out any data they would need to fill out: TERF_Controller_CreateNew contr = new TERF_Controller_CreateNew(); contr.MR....=....; – sfdcfox Aug 24 '18 at 11:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.