+ Start a Discussion
DevAccountDevAccount 

Help needed on writing test method.

Hi,

 

Help needed on writing test method.

 

I checked the line coverage and found the below red fonted were not covered, Please help to write test cases for the Upsert statement.

 

 

public with sharing class TallySheetItems {

private ApexPages.StandardController controller {get; set;}

Tally_Sheet__c TS;
String recordId;

public TallySheetItems(ApexPages.StandardController stdController)
{
recordId = stdController.getId();
TS = (Tally_Sheet__c) stdController.getRecord();
this.controller=stdController;
}

List<TS_NASAAC_Item__c> TallySheetItemList;

public PageReference reset()
{
TallySheetItemList= [select ID, MELT__c, BND__c, GR_WT_KGS__c, Tare__c,
NET_WT_KGS__c, Melt_Date__c, INGOTS__c, Ref__c, IsDelete d__c from TS_NASAAC_Item__c where Tally_Sheet_Ref__c = :TS.ID and IsDeleted__c = false order by CreatedDate limit 100 ];
return null;
}

public List<TS_NASAAC_Item__c> getTallySheetItems()
{
if(TallySheetItemList== null) reset();
return TallySheetItemList;
}

public PageReference saveItem()
{
upsert TallySheetItemList;
reset();
return null;
}

public PageReference add()
{
TS_NASAAC_Item__c TSI = new TS_NASAAC_Item__c();
TSI.Tally_Sheet_Ref__c = TS.ID;
TallySheetItemList.add(TSI);
return null;
}


/* Test Case */

static testMethod void myUnitTest() {
PageReference PageRef = Page.vfTallySheet;
test.setCurrentPage(PageRef);
Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Tally_Sheet__c());
TallySheetItems controller = new TallySheetItems(sc);
controller.getTallySheetItems();
controller.reset();
controller.add();
controller.saveItem();
}

/* Test Case ends....*/


}



Any help would certainly appreciated.

Thanks
Dev

 

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Try the sample code given below :

 

            Tally_Sheet__c TS = new Tally_Sheet__c();

                                                insert TS;

            Tally_Sheet_Ref__c tcs = new Tally_Sheet_Ref__c();

                                                insert tcs;

            List<TS_NASAAC_Item__c>  wraptc = new   List<TS_NASAAC_Item__c> ();

            TS_NASAAC_Item__c tc = new TS_NASAAC_Item__c(Tally_Sheet_Ref__c = TS.id, IsDeleted__c = false);

            wraptc.add(tc);

            upsert wraptc;

 

Did this answer your question? if so, please mark it solved.