0

I delete 1 record from table. How to pass into test method selectedMR(this record Id) and cover in test method from controller below?

VF PAGE:

<apex:repeat value={!mrItems} var="mrItem">
<apex:column>
            <apex:facet name="header">Deleting</apex:facet>
            <apex:commandButton value="Del" action="{!deleteMR}" reRender="form">
                <apex:param name="mritemId" value="{!mrItem.id}" assignTo="{!selectedMR}"/>
            </apex:commandButton>
        </apex:column>
</apex:repeat>

CONTROLLER

public void deleteMR() {
    if (selectedMR == null) {
        return;
    }

    TERF_MR__c tobeDeleted = null;
    for (TERF_MR__c mr : mrList)
        if (mr.Id == selectedMR) {
            if (mr.TERF_MR_Status__c != 'Approved') {
                tobeDeleted = mr;
                break;
            }
            else {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'You can\'t delete approved month report'));
            }
        }

    if (tobeDeleted != null) {
        delete tobeDeleted;
        loadData();
    }

}

TEST

    static testMethod void test1() {
    test.startTest();
    TERF_MR__c MR = new TERF_MR__c();
    Insert MR ;
    System.assert([SELECT Name FROM TERF_MR__c WHERE Id = :MR.Id].Name != null);
    PageReference testpage = new pageReference('/apex/TERF_Home');
    testpage.getParameters().put('selectedMR', mr.Id);
    ApexPages.currentPage().getParameters().put('id', MR.id);
    TERF_Controller_Home contr = new TERF_Controller_Home();
    contr.gotoTERF_CreateNew();
    contr.deleteMR();
    contr.goToSystem();
    contr.save();
    test.stopTest();
}

i need cover this:

  • Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests. - How to Write Good Unit Tests – Adrian Larson Aug 2 '18 at 16:40
  • sorry, Adrian, is there something wrong in my test method? – Ivan S. Aug 3 '18 at 6:29
2

For your test, set it directly on the controller:

TERF_Controller_Home contr = new TERF_Controller_Home();
contr.selectedMR = mr.Id;
...
contr.deleteMR();

Not sure, but you can probably also do it by naming the parameter to match this:

<apex:param name="mritemId" value="{!mrItem.id}" assignTo="{!selectedMR}"/>

i.e.:

testpage.getParameters().put('mritemId', mr.Id);

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.