测试类没有覆盖投掷错误

时间:2014-12-05 07:25:28

标签: salesforce

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;
    }
}

测试类:

@isTest
public class Teststatus {

    public  static testMethod void teststatus() {
        Candidates__c opp = new Candidates__c(First_Name__c = 'test12', Email__c = 'testfdc@gmail.com', 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:SOBJECT的附件:Candidates__c     Stack Trace Class.status。:第8行,第1列     Class.Teststatus.teststatus:第26行,第1列

  

1 个答案:

答案 0 :(得分:0)

麻烦就在这一行

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

因为您传递的myAttach1类型为Attachment,但您的控制器需要Candidates__c记录此处。