function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gbrowngbrown 

Need help increasing code coverage

I'm having a difficult time to get my code coverage increased (I'm still a newb on the dev end).

My class is working and the test class passes, but I'm not able to piece it all together.  Any help?

 

My Class to display values on a VF page...

public class ascSummary{

    public ascSummary() {

    }


    public ascSummary(ApexPages.StandardController controller) {

    }


        public Visit__c getVisit()
    {
        
        //Retrieve Audit Results based on Id parameter of this page
        return [Select Id,
                Visit__c.Account__c,
                Visit__c.Contact__c,
                Visit__c.Visit_Date__c,
                Visit__c.Pre_audit_score__c,
                Visit__c.Workshop_Score1__c,
                Visit__c.Total_3_0v2__c,
                Visit__c.Total_4_0v2__c,
                Visit__c.Total_5_0v2__c,
                Visit__c.X1_0__c,
                Visit__c.X2_0__c,
                Visit__c.X3_0__c,
                Visit__c.X4_0__c,
                Visit__c.X5_0__c,
                Visit__c.Total_Percent__c,
                Visit__c.Total_Audit_Score__c
                from Visit__c j
                where Id = 
:ApexPages.currentPage().getParameters().get('id') ];
    }
}

 My test class..

@isTest
private class ascSummaryTestClass {
     static testMethod void validateascSummary() {
         visit__c v = new visit__c (Account__c='001c000000Qm4kp', Contact__c = '003c000000N7Bl0');
         insert v;
        
          // Retrieve the contact from the visit
        v = [SELECT Contact__c FROM Visit__c WHERE Id =:v.Id];
    System.debug('Contact after visit created: ' + v.Contact__c);  
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

So I don't think you need a custom controller at all then...looks like you can just use the Standard Controller - even if the users have edit permissions, the page wil render as defined below as output text:

 

So change the page heading to:

<apex:page StandardController="Visit__c" showHeader="false">

 

And update all the Visit references to be Visit__c e.g. 

<apex:outputField value="{!Visit__c.Contact__c}"/>  

 

When you call the page with Id= [ValidId], it will render for that record automatically, with no additional code.

 

Also, because it is a Standard Controller, you can also build a custom button that you can call directly from a detail page that will automatically render it in the correct context - just choose VisualForce page as the option

 

All Answers

k_bentsenk_bentsen

You could either change your class so that your return statement is all on one line, or change your query in your test class retrieves all the fields in the above mentioned return statment.

 

Also, I would recommend that you do not hard code Ids in your test class. Instead, create and insert dummy records and use the Ids from your dummy records to create a test visit__c record.

BritishBoyinDCBritishBoyinDC

Couple of questions, and then I think we can get it working fine...

 

Is this an extension to the Visit__c  controller? Looks it is supposed to be, but if so, there are a few pieces missing. Once I know that, I can give you a working example of the extension and the test

gbrowngbrown
Visit is a custom object, used a standard controller on the original VF page.

Sent from my iPhone
BritishBoyinDCBritishBoyinDC

Okay - can you post the basic page then...I think I need to see the use case for this class... 

gbrowngbrown

here is the visualforce page that I created to display results only, did not want the users to be able to edit the page.

 

<apex:page Controller="ascSummary" showHeader="false">

  
<apex:pageBlock title="Audit Summary">

<h1 style="font-family:verdana;">Account     <apex:outputField value="{!Visit.Account__c}"/>       
Contact:  <apex:outputField value="{!Visit.Contact__c}"/>       
Visit Date:  <apex:outputText value="{!Visit.Visit_Date__c}"/><br></br></h1>

<br></br><br></br>

<!--- test table --->
<apex:pageBlockSection title="Audit Summary"  collapsible="False" ></apex:pageBlockSection>
<div align="left">
  <table width="50%" border="0">
    <tr>
      <th scope="col"><div align="left">Area</div></th>
      <th scope="col"><div align="center">Maximum Score</div></th>
      <th scope="col"><div align="center">Actual Score</div></th>
      <th scope="col"><div align="center">%</div></th>
    </tr>
    <tr>
      <td><div align="left">
        <apex:outputText value="Pre Audit"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="140"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value=" {!Visit.Pre_audit_score__c}" label=""/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="{!Visit.X1_0__c}" label="" style="{!IF(Visit.X1_0__c <= 50,'color:black;font-weight: bold; background-color:red',IF(Visit.X1_0__c >= 76,'color:black;font-weight: bold; background-color:rgb(0,176,80)',IF(Visit.X1_0__c >= 61,'color:black;font-weight: bold; background-color:rgb(155,187,89)',IF(Visit.X1_0__c >= 51,'color:black;font-weight: bold; background-color:rgb(255,192,0)', 'color:blue'))))}"/>     </div></td>
    </tr>
    <tr>
      <td><div align="left">
        <apex:outputText value="Work Shop"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="120"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value=" {!Visit.Workshop_Score1__c}" label=""/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="{!Visit.X2_0__c}" label="" style="{!IF(Visit.X2_0__c <= 50,'color:black;font-weight: bold; background-color:rgb(255,0,0)',IF(Visit.X2_0__c >= 76,'color:black;font-weight: bold; background-color:rgb(0,176,80)',IF(Visit.X2_0__c >= 61,'color:black;font-weight: bold; background-color:rgb(155,187,89)',IF(Visit.X2_0__c >= 51,'color:black;font-weight: bold; background-color:rgb(255,192,0)', 'color:blue'))))}"/>
      </div></td>
    </tr>
    <tr>
      <td><div align="left">
        <apex:outputText value="Quality & Metrics"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="100"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value=" {!Visit.Total_3_0v2__c}" label="" />
      </div></td>
      <td><div align="center">
        <apex:outputText value="{!Visit.X3_0__c}" label="" style="{!IF(Visit.X3_0__c <= 50,'color:black;font-weight: bold; background-color:red',IF(Visit.X3_0__c >= 76,'color:black;font-weight: bold; background-color:rgb(0,176,80)',IF(Visit.X3_0__c >= 61,'color:black;font-weight: bold; background-color:rgb(155,187,89)',IF(Visit.X3_0__c >= 51,'color:black;font-weight: bold; background-color:rgb(255,192,0)', 'color:blue'))))}"/>
      </div></td>
    </tr>
    <tr>
      <td><div align="left">
        <apex:outputText value="Additional Services Provided"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="40"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="  {!Visit.Total_4_0v2__c}" label=""/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="{!Visit.X4_0__c}" label="" style="{!IF(Visit.X4_0__c <= 50,'color:black;font-weight: bold; background-color:red',IF(Visit.X4_0__c >= 76,'color:black;font-weight: bold; background-color:rgb(0,176,80)',IF(Visit.X4_0__c >= 61,'color:black;font-weight: bold; background-color:rgb(155,187,89)',IF(Visit.X4_0__c >= 51,'color:black;font-weight: bold; background-color:rgb(255,192,0)', 'color:blue'))))}"/>
      </div></td>
    </tr>
    <tr>
      <td><div align="left">
        <apex:outputText value="Discretionary Score"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="100"/>
      </div></td>
      <td><div align="center">
        <apex:outputText value=" {!Visit.Total_5_0v2__c}" label=""/>
      </div></td>
      <td><div align="center">
        <apex:outputText value="{!Visit.X5_0__c}" label="" style="{!IF(Visit.X5_0__c <= 50,'color:black;font-weight: bold; background-color:red',IF(Visit.X5_0__c >= 76,'color:black;font-weight: bold; background-color:rgb(0,176,80)',IF(Visit.X5_0__c >= 61,'color:black;font-weight: bold; background-color:rgb(155,187,89)',IF(Visit.X5_0__c >= 51,'color:black;font-weight: bold; background-color:rgb(255,192,0)', 'color:blue'))))}"/>
      </div></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td><strong>Total</strong></td>
      <td><div align="center"><strong>500</strong></div></td>
      <td><div align="center">
        <strong>
        <apex:outputText value=" {!Visit.Total_Audit_Score__c}" label=""/>
      </strong></div></td>
      <td><div align="center">
        <strong>
        <apex:outputText value=" {!Visit.Total_Percent__c}" label="" style="{!IF(Visit.X1_0__c <= 50,'color:black;font-weight: bold; background-color:red',IF(Visit.X1_0__c >= 76,'color:black;font-weight: bold; background-color:rgb(0,176,80)',IF(Visit.X1_0__c >= 61,'color:black;font-weight: bold; background-color:rgb(155,187,89)',IF(Visit.X1_0__c >= 51,'color:black;font-weight: bold; background-color:rgb(255,192,0)', 'color:blue'))))}"/>
      </strong></div></td>
    </tr>
  </table>
</div>

</apex:pageblock>

</apex:page>

 

BritishBoyinDCBritishBoyinDC

So I don't think you need a custom controller at all then...looks like you can just use the Standard Controller - even if the users have edit permissions, the page wil render as defined below as output text:

 

So change the page heading to:

<apex:page StandardController="Visit__c" showHeader="false">

 

And update all the Visit references to be Visit__c e.g. 

<apex:outputField value="{!Visit__c.Contact__c}"/>  

 

When you call the page with Id= [ValidId], it will render for that record automatically, with no additional code.

 

Also, because it is a Standard Controller, you can also build a custom button that you can call directly from a detail page that will automatically render it in the correct context - just choose VisualForce page as the option

 

This was selected as the best answer