You need to sign in to do that
Don't have an account?

How to cover java script variables in test class
Hi All,
I am passing javascript value from vf page to controller.But I was unable to cover test class.
Here is my vf code and apex class code:
Vf Page:
function showAlert(msg){
var X = document.getElementById(msg).value;
Batch1(X);
}
<input type="Button" onclick="showAlert('{!$Component.Name}');" value="Add Batch" valign="top" /></td>
<apex:actionFunction name="Batch1" action="{!AddBatch}" reRender="pb1,errormsg" >
<apex:param id="X" name="X" value="" />
</apex:actionFunction>
Apex class:
public void AddBatch()
{
getdata =[Select id,Name,Client_P_N__c,Description__c,Standard_Estimated_Lead_Time__c,Other_Lead_Time__c,Quote__c from Quote_Line_Item__c where Quote1__c=: Id] ;
for(Quote_line_item__c q : getdata )
{
for(integer i=0;i<lstInner.size();i++)
{
if(lstInner[i].id == null)
{
if(q.name == lstInner[i].Name )
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Nicomatic P/N'+' ' +lstInner[i].Name +' '+'is already existed with the same Quote');
ApexPages.addMessage(myMsg);
return;
}
}
}
}
for(integer i=0;i<lstInner.size();i++)
{
if(lstInner[i].id == null)
{
insert lstInner[i];
}
else
if(lstInner[i].id != null)
{
update lstInner[i];
}
}
system.debug('---X value---------'+ApexPages.CurrentPage().getParameters().get('X'));
// "in the below line i am getting error as List has no rows for assignment to SObject"
Quote_Line_Item__c q = [select id,Description__c from Quote_Line_Item__c where name =: ApexPages.CurrentPage().getParameters().get('X') AND Quote1__c =: Id LIMIT 1 ];
Batch__c b = new Batch__c();
b.Quote_Line_Item__c = q.Id;
lstAcct1.add(b);
System.debug(lstAcct1);
System.debug(lstAcct1.size());
try
{
upsert lstAcct1;
}
catch(DMLException e)
{
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, e.getMessage() );
ApexPages.addMessage(msg);
}
innerId= new set<Id>();
for(Quote_Line_item__c q1: lstInner) {
innerId.add(q1.Id);
}
childId= new set<Id>();
for(batch__c b1: lstAcct1) {
childId.add(b1.Id);
}
}
If I call the AddBatch() in test class I am Getting error as "List has no rows for assignment to SObject"
Can any one please guide me....
Thanks in advance,
You will have to manually add a url parameter in your test code. Because your query needs that filter, only then it will retrieve you records.
Create a quote with name = 'test';
then in your test code, add this code first after record creation:
ApexPages.CurrentPage().getParameters().put('X', 'test');
// now in the query, it will have this name and so it will return you a record.