1

我无法上传我的包,因为测试覆盖率低于 75%。如何提高覆盖率。如何编写触发器的测试方法。请帮忙。

我试过这段代码:这是我的控制器类:

    public class MyController{
         public List<Merchandise__c> merch{get;set;}
         public Merchandise__c toDelete{get;set;}


         public MyController(){


 merch = [SELECT ID,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c];
    }

    public pageReference del(){
        string id = Apexpages.currentpage().getParameters().get('merchandiseId');
        toDelete = [SELECT Id,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c WHERE Id =: id];     
        delete toDelete;
        return null;
    }

     public pageReference add(){
       Merchandise__c  item = new Merchandise__c(
           Name = 'Enter Name',
           Description__c = 'Enter Description',
           Price__c = 0.00,
           Total_Inventory__c = 0
       );
        merch.add(item);
        insert item;
        return null;
    }

    public PageReference save(){   
        string id = Apexpages.currentpage().getParameters().get('merchandiseId');        
        Merchandise__c toUpdate = [SELECT Name, Description__c, Price__c FROM Merchandise__c WHERE ID =: id];          
        update toUpdate;  
        return null;
      }

}

我已经创建了这个测试类:

 @isTest

Public Class demoTest{
     static testMethod void checkDatatableData() {         
          List<Merchandise__c> merch;

          Test.startTest();
          merch = [SELECT ID,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c];
          Test.stopTest();
   }


    static testMethod void checkAdd() {

           Merchandise__c  item = new Merchandise__c(
           Name = 'Enter Name',
           Description__c = 'Enter Description',
           Price__c = 0.00,
           Total_Inventory__c = 0
        );

            Test.startTest(); 
            insert item;
            System.assertNotEquals(null,item.Id);
            List<Merchandise__c> merchItem = [SELECT Id FROM Merchandise__c WHERE Id =: item.Id];
            System.assertEquals(1,merchItem.size());
            Test.stopTest();
      }   


      static testMethod void checkUpdateDelete() { 

               Merchandise__c  testmerch = new Merchandise__c(
               Name = 'NewProduct',
               Description__c = 'this is anew product',
               Price__c = 20.00,
               Total_Inventory__c = 100
               );

              Test.startTest(); 
              insert testmerch;
              system.assertNotEquals(null,testmerch.Id);
              testmerch.Price__c = 40.00;
              update testmerch;

              Merchandise__c updatedmerch = [SELECT Id,Price__c FROM Merchandise__c WHERE Id =: testmerch.Id];          
              system.assertEquals(40.00,updatedmerch.Price__c);

              Merchandise__c  toDelete = [SELECT ID FROM Merchandise__c WHERE Id =: testmerch.Id];
              system.assertNotEquals(null,toDelete.Id);
              delete toDelete;
              Test.stopTest(); 
         }    

  }

你能告诉我我需要做哪些改变来提高覆盖率吗?

4

1 回答 1

1

这个链接应该告诉你你需要知道什么。

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

基本上,您需要在测试方法中复制触发器的功能,例如,如果您有一个更新后触发器案例,当它从 Open 移动到 Closed 时运行,您首先需要为您的触发器创建一个测试方法,然后在测试中方法插入案例记录进行测试,这可能还需要联系人或帐户等,然后将案例的值更改为已关闭,然后运行更新方法。这非常简单,但这就是想法。因此,在编写测试类时,请尝试考虑您的触发器和类做什么,然后尝试自动化用户在您的测试方法中执行的操作,确保涵盖您的所有基础,包括顶点应该触发异常的位置。

尽量避免使用硬编码的 ID。

这些链接也可能有帮助

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

于 2013-10-04T06:00:24.930 回答