【问题标题】:Test Class not Covering Throwing an Error测试类不涵盖引发错误
【发布时间】:2014-12-05 07:25:27
【问题描述】:

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:Attachment 到 SOBJECT:Candidates__c 的转换无效 堆栈跟踪 Class.status.:第 8 行,第 1 列 Class.Teststatus.teststatus:第 26 行,第 1 列

【问题讨论】:

    标签: salesforce


    【解决方案1】:

    问题出在这一行

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

    因为您传递了一个 myAttach1 类型为 Attachment 但您的控制器希望在此处记录 Candidates__c

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      相关资源
      最近更新 更多