+ Start a Discussion
CB312CB312 

Passing Variables in the VF page to a Test Class

Hi,

 

I've built a VF page and am having problems initiing my variables in the test class. I can't get the code to execute so I am only getting 10% coverage. Any ideas?

 

VF Page:

 

This is where the java vars are getting put in.

 

 

<apex:pageBlock title="Search (beta)" mode="edit" id="criteria">
 
      <script type="text/javascript">
        function doSearch() {
        setTimeout(1550);
        searchServer(
        document.getElementById("account").value, document.getElementById("categories").options[document.getElementById("categories").selectedIndex].value,
Then there is a table that has this code:
      <tr>
        <td style="font-weight:bold;">Account<br/>
        <input type="text" id="account" onkeyup="doSearch();"/>
        </td>
      </tr>
Apex Code:
public PageReference runSearch() {
String account = Apexpages.currentPage().getParameters().get('account');
if (!account.equals(''))
      soql += ' AND Custom_Obj__r.Account_Name__r.name LIKE \''+String.escapeSingleQuotes(account)+'%\'';
runQuery();
return null; 
public void runQuery() {
 
    try {
      Deals = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 50');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
Apex Test Code:
string theacctname= 'test acct';
PageReference pageRef = Page.SearchPage;
                       Test.setCurrentPage(pageRef); 
                       pageRef.getParameters().put('account', theacctname);
  
      SearchController con = new SearchController();
                       con = new SearchController();
                       con.runquery();
                       con.runsearch();

 

help?
Thanks,
Chris

 

kiranmutturukiranmutturu

first create test record in your test class then pass the name of the record as query string..... then it will work out for you,...

 

 

as you are sending 'test acct' as a string varaible .. this may exist in the database or may not be ..