测试类需要100%的结果:在PageReference中

时间:2015-05-05 13:02:29

标签: class testing salesforce apex

public with sharing class contactInsert {
    public contact myC{get;set;}
    public contactInsert(){
        myC = new Contact();
    }
    public PageReference iSave(){
        insert myC;
        return new pageReference('/'+myC.id);
    }
}

网页

<apex:page controller="contactInsert"><br>
    <apex:form ><br>
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!myC.firstname}"/>
                <apex:inputField value="{!myC.lastname}"/>
                <apex:inputField value="{!myC.email}"/>
                <apex:inputField value="{!myC.phone}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!iSave}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

我创建了Test Class但它无法恢复PageReference:

@isTest
public with sharing class TestcontactInsert {
    @isTest
    private static void myTest(){
        contact c = new contact(firstname='fnam',lastname='lnam',email='a@a.com',phone='89898989');
        insert c;

         PageReference pageRef = Page.InsertContact;
         Test.setCurrentPage(pageRef);

         ContactInsert ci = new ContactInsert();
         ci.iSave();
    }
} 

1 个答案:

答案 0 :(得分:0)

您需要myC上的ci联系人属性到尚未插入的联系人。如果您不这样做,您将从insert myC;行获得Null参考例外。