2014-12-05 101 views
1

测试类不覆盖引发错误

public class status { 
    public Candidates__c applicant; 
    public Blob resume {get; set;} 
    public String contentType {get; set;} 
    public String fileName {get; set;} 

    public status(ApexPages.StandardController stdController) { 
     this.applicant = (Candidates__c)stdController.getRecord(); 
    } 
    public PageReference saveApplication() { 
     try { 
      insert(applicant); 
     } catch (System.DMLException e) { 
      ApexPages.addMessages(e); 
      return null; 
     } 
     if (resume != null) { 
      Attachment attach = new Attachment(); 
      attach.Body = resume; 
      attach.Name = filename; 
      attach.ContentType = contentType; 
      attach.ParentID = applicant.id; 
      try { 
       insert(attach); 
      } catch (System.DMLException e) { 
       ApexPages.addMessages(e); 
       return null; 
      } 
     } 
     //PageReference p = new ApexPages.StandardController(applicant).view(); 
     PageReference p = Page.Resume_Parsing; 
     p.setRedirect(true); 
     return p; 
    } 
} 

测试类:从运行时类型的sObject的转换无效:

@isTest 
public class Teststatus { 

    public static testMethod void teststatus() { 
     Candidates__c opp = new Candidates__c(First_Name__c = 'test12', Email__c = '[email protected]', Last_Name__c = 'fff', Phone__c = '9999999999'); 
     insert opp; 

     Attachment myAttach1 = new Attachment(); 
     myAttach1.ParentId = opp.id; 
     myAttach1.name = 'Resume_Parsing.pdf'; 

     myAttach1.body = blob.valueof('test'); 

     insert myAttach1; 

     status atc = new status(new ApexPages.StandardController(opp)); 
     system.debug('%%%%%' + atc); 
     PageReference pageRef = Page.Resume_Parsing; 
     pageRef.getParameters().put('id', String.valueOf(opp.Id)); 
     Test.setCurrentPage(pageRef); 
     Blob b; 

     ApexPages.currentPage().getParameters().put('id', opp.id); 
     status atc1 = new status(new ApexPages.StandardController(myAttach1)); 

     atc.saveApplication(); 

     return; 
    } 

} 

错误消息System.TypeException附到的sObject:Candidates__c 堆栈跟踪Class.status .:第8行第1列 Class.Teststatus.teststatus:第26行第1列

回答

0

麻烦的是在这条线

status atc1 = new status(new ApexPages.StandardController(myAttach1));

因为你传递一个myAttach1其类型为Attachment但你的控制器期望Candidates__c记录在这里。