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