类:
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();
}
}
答案 0 :(得分:0)
您需要myC
上的ci
联系人属性到尚未插入的联系人。如果您不这样做,您将从insert myC;
行获得Null参考例外。