+ Start a Discussion
Taresh PandeyTaresh Pandey 

Need help to write "Test Class" for the RemoteAction

// This method is use to create chart Records (Bulky method) 
// INPUT: Array of Sobject (Chart) with field data. 
// OUTPUT: return ID if inserted without error return null if inertion failes. 
//
@RemoteAction
public static String insertChart(List<Chart> RemoteCharts){
List<Lean__Chart__c> NewCharts= new List<Lean__Chart__c>();
    for(Chart Chart: RemoteCharts){
    Lean__Chart__C Newchart= new Lean__Chart__c();
    Newchart.Name =Chart.Name;
    Newchart.Lean__DeveloperName__c = Chart.DeveloperName;
    Newchart.Lean__Filter__c = Chart.Filter;
    Newchart.Lean__Format__c = Chart.Format;
    Newchart.Lean__GUID__c = Chart.Id;
    Newchart.Lean__Height__c = Chart.Height;
    Newchart.Lean__Left__c = Chart.Left;
    Newchart.Lean__LockState__c = Chart.LockState;
    Newchart.Lean__ReportId__c = Chart.ReportID;
    Newchart.Lean__Top__c = Chart.Top;
    Newchart.Lean__ValueStream__c = (Chart.ValueStreamID==''?null:Chart.ValueStreamID);
    Newchart.Lean__Width__c = Chart.Width;
    Newchart.Lean__X__c = Chart.X;
    Newchart.Lean__Y__c = Chart.Y;
    Newchart.Lean__CmpID__c =Chart.CmpID;
    Newchart.Lean__Size__c = Chart.Size;    
    Newchart.Lean__ChartURL__c = Chart.ChartURL;
    Newchart.Lean__JSONData__c = Chart.JSONData;
    NewChart.Lean__OnlyShowChart__c = Chart.OnlyShowChart;    
        NewCharts.add(Newchart);
}
    List<Database.Upsertresult> uResults;
        try {
          Schema.Sobjectfield ExternField = Lean__Chart__C.Fields.Lean__GUID__c;
          uResults = Database.upsert(NewCharts, ExternField, true); 
        } catch (DmlException e){
            System.debug(e.getMessage());
            return '';
            
        }

return NewCharts[0].Id ;
}

Regards'
Taresh
Best Answer chosen by Taresh Pandey
Abhishek BansalAbhishek Bansal
Hi Taresh,

Please find test class below :
@isTest(seeAllData=false)
private class chartTest
{
	static testMethod void testRemoteActionMethod(){
		List<Chart> testChartList = new List<Chart>();//As per my understanding Chart is the name of your some custom object so please replace it with the API name of that object
		testChartList.add(new Chart(Name = 'Test Chart 1'));
		testChartList.add(new Chart(Name = 'Test Chart 2'));
		testChartList.add(new Chart(Name = 'Test Chart 3'));
		testChartList.add(new Chart(Name = 'Test Chart 4'));
		testChartList.add(new Chart(Name = 'Test Chart 5'));
		
		insert testChartList;
		
		Test.startTest();
		YourClassName.insertChart(testChartList);//Replace YourClassName with the Name of your class in which you have written this method
		Test.stopTest();
		
		List<Lean__Chart__c> testSizeList = new List<Lean__Chart__c>([Select id from Lean__Chart__c]);
		system.assertEquals(5,testSizeList.size());
	}
}
Let me know if you need any help.

Regards,
Abhishek
 

All Answers

Abhishek BansalAbhishek Bansal
Hi Taresh,

Please find test class below :
@isTest(seeAllData=false)
private class chartTest
{
	static testMethod void testRemoteActionMethod(){
		List<Chart> testChartList = new List<Chart>();//As per my understanding Chart is the name of your some custom object so please replace it with the API name of that object
		testChartList.add(new Chart(Name = 'Test Chart 1'));
		testChartList.add(new Chart(Name = 'Test Chart 2'));
		testChartList.add(new Chart(Name = 'Test Chart 3'));
		testChartList.add(new Chart(Name = 'Test Chart 4'));
		testChartList.add(new Chart(Name = 'Test Chart 5'));
		
		insert testChartList;
		
		Test.startTest();
		YourClassName.insertChart(testChartList);//Replace YourClassName with the Name of your class in which you have written this method
		Test.stopTest();
		
		List<Lean__Chart__c> testSizeList = new List<Lean__Chart__c>([Select id from Lean__Chart__c]);
		system.assertEquals(5,testSizeList.size());
	}
}
Let me know if you need any help.

Regards,
Abhishek
 
This was selected as the best answer
Taresh PandeyTaresh Pandey
Thanks Abhishk ... :)