【问题标题】:need help on how to achieve 100% in the test class需要关于如何在测试课上达到 100% 的帮助
【发布时间】:2015-12-02 22:10:35
【问题描述】:

我一直在尝试编写 100% 覆盖率的测试类。我很难理解如何调用标准控制器并调用参数化构造函数还将页面设置为当前页面作为当前页面。 在帐户视图页面上,我已经覆盖了自定义对象相关列表上的新按钮。当我编辑记录时,我可以使用相同的类。在编辑记录视图页面和新记录视图页面上,我都可以检索帐户 ID。目前我能够达到 65% 的覆盖率。我不确定我做错了什么。如果可能,请提供帮助。我附上了下面的代码。

谢谢 加亚特里

//my Class

public class NewTaxExempt{

    public TaxExempt__c obj {get; set;}   
    public String selectedIso {get;set;}

    public List<selectOption> isoCodes {

        get {
            List<selectOption> options = new List<selectOption>();

            for (State__c iso : State__c.getAll().values())
            options.add(new SelectOption(iso.State_ISO__c,iso.State_Name__c+' - '+iso.State_ISO__c));

            return options;

        }

        set;
    }

    // onload standard controller will be called.
    public NewTaxExempt(ApexPages.StandardController controller) {

        ID idte=controller.getRecord().id;
        if(idte==null){
            obj=(TaxExempt__c)controller.getRecord();
        }
        else{
            obj= [Select Account__c, Certificate_ID__c, Certificate_Type__c,Description__c, Issuing_Jurisdiction__c,Status__c,Tax_Exempt_Effective_Date__c,Tax_Exempt_Expiration_Date__c from TaxExempt__c where id=:idte];         
        }


    }

    //on click on cancel button
    public PageReference cancel() {
        return new PageReference('/'+obj.account__c);
    } 

    //on click on Save button
    public PageReference save() {

        obj.Issuing_Jurisdiction__c=selectedIso;
        if(obj.id==null){        
            insert obj;
        }
        else{
            update obj;
        }
        return new PageReference('/'+obj.account__c);
    }
}
//my test class

@isTest
public class NewTaxExemptTest{

    public static testMethod void whenIssueJurisdictionIsBlankDefaultValueIsSet(){

        try{    

            BET_ObjectFactory.generateTaxExemptRequirements();
            Account aobj = ( Account)TestFactory.createSObject(new Account(Name='test acc gs250144'),true);
            PageReference ref = Page.NewTaxExempt;
            Test.setCurrentPage(ref);
            TaxExempt__c obj=new TaxExempt__c();
            obj.account__c=aobj.Id;
            obj.Issuing_Jurisdiction__c='RI';
            ApexPages.StandardController sc = new ApexPages.StandardController(obj);
            NewTaxExempt ob=new NewTaxExempt(sc);
            ob.selectedIso='RI'; 
            ob.obj=obj; 

            ob.save(); 

            System.assertEquals(obj.Issuing_Jurisdiction__c,null);


        List<State__c> stList=new List<State__c>();

        State__c st=new State__c();
        st.Name='ST_11009';        
        st.State_Name__c='Alabama';
        st.State_ISO__c='AL';
        st.State_Country_Name__c='United States';
        st.State_Country_ISO__c='US';
        stList.add(st);

        State__c st1=new State__c();
        st1.Name='ST_11008';
        st1.State_Name__c='Alabama';
        st1.State_ISO__c='AL';
        st1.State_Country_Name__c='United States';
        st1.State_Country_ISO__c='US';
        stList.add(st1);

        State__c st2=new State__c();
        st2.Name='ST_11019';        
        st2.State_Name__c='Alaska';
        st2.State_ISO__c='AK';
        st2.State_Country_Name__c='United States';
        st2.State_Country_ISO__c='US';
        stList.add(st2);

        State__c st3=new State__c();
        st3.Name='ST_12009';        
        st3.State_Name__c='Arizona';
        st3.State_ISO__c='AZ';
        st3.State_Country_Name__c='United States';
        st3.State_Country_ISO__c='US';
        stList.add(st3);

        State__c st4=new State__c();
        st4.Name='ST_11309';
        st4.State_Name__c='Arkansas';
        st4.State_ISO__c='AR';
        st4.State_Country_Name__c='United States';
        st4.State_Country_ISO__c='US';
        stList.add(st4);

        State__c st5=new State__c();
        st5.Name='ST_21009';        
        st5.State_Name__c='California';
        st5.State_ISO__c='CA';
        st5.State_Country_Name__c='United States';
        st5.State_Country_ISO__c='US';
        stList.add(st5);

        State__c st6=new State__c();
        st6.Name='ST_23';        
        st6.State_Name__c='California';
        st6.State_ISO__c='';
        st6.State_Country_Name__c='';
        st6.State_Country_ISO__c='';
        stList.add(st6);

        insert stList;

        List<State__c> allStates = [select State_Name__c, State_ISO__c, State_Country_Name__c, State_Country_ISO__c 
                                    from State__c
                                    order by State_Name__c];

        for(State__c stt : allStates){
            if(stt.State_ISO__c == '' || stt.State_ISO__c == null){
                ob.isoCodes.add(new SelectOption(stt.State_ISO__c, stt.State_Name__c));
            }
            else{
                ob.isoCodes.add(new SelectOption(stt.State_ISO__c, stt.State_Name__c));                
            }

        }        
        System.assertNotEquals(ob.isoCodes.size(),0);


        }catch(Exception e){

            System.debug('Gayatri exception :'+e);
        }   
    }

    public static testMethod void whenUserClicksCancel(){

        BET_ObjectFactory.generateTaxExemptRequirements();
        Account aobj = ( Account)TestFactory.createSObject(new Account(Name='test acc gs250144'),true);

        // PageReference ref = new PageReference(pageName)
        // ref.getParameters().put('TaxExempt__c',obj.Id);
        // Test.setCurrentPage(ref);

        PageReference ref = Page.NewTaxExempt;
        Test.setCurrentPage(ref);
        TaxExempt__c obj=new TaxExempt__c();
        ApexPages.StandardController sc = new ApexPages.StandardController(obj);
        NewTaxExempt ob=new NewTaxExempt(sc);
        ob.cancel();  

    }    
}

【问题讨论】:

  • 我需要有关如何覆盖列表获取设置变量的帮助: public List isoCodes { get { List options = new List(); for (State__c iso : State__c.getAll().values()) options.add(new SelectOption(iso.State_ISO__c,iso.State_Name__c+' - '+iso.State_ISO__c));退货选项; } 放; }
  • 并且在构造函数中我可以设置 ID 值,因此不包括 else 部分的代码:public NewTaxExempt(ApexPages.StandardController controller) {

标签: salesforce visualforce apex test-class


【解决方案1】:

如果您知道测试覆盖率中缺少代码的哪一部分,那将会容易得多。

  1. 转到开发者控制台并运行您的测试
  2. 在页面底部选择测试选项卡
  3. 从右侧找到您的测试(标题:总体代码覆盖率)
  4. 双击您的测试

未覆盖的部分将显示为红色。现在尝试为他们编写测试。

如果你想覆盖你的类属性,你所要做的就是:

@isTest static void myPropertiesTest(){

    MyClass obj = new MyClass();
    obj.myProperty = SomeValue;
    // now assert a function in you class that uses this property. 

    // an easy and dumb way is to just assert the property. 
    System.assertEquals(SomeValue, obj.myProperty);
}

【讨论】:

  • 感谢 Daniel,我能够看到测试覆盖率。我需要有关如何覆盖 get set 变量的帮助 public List isoCodes { get { List options = new List(); for (State__c iso : State__c.getAll().values()) options.add(new SelectOption(iso.State_ISO__c,iso.State_Name__c+' - '+iso.State_ISO__c));退货选项; } 放; }
  • @user3161203 如果你想覆盖你的类的属性,只需初始化它们并在你的断言中读回它们。我已经更新了解决方案。
猜你喜欢
  • 2011-12-19
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多