You need to sign in to do that
Don't have an account?
Developer.mikie.Apex.Student
Test Code Failing at 90%
Hey guys, I am attempting to test my extension. It is getting 90% coverage and yet it is still failing funnily enough (I thought anything over 75% was a pass). I know why it is failing, I am just not sure how to rectify it.
Basically, I have a VF page which is for a survey object that I am creating. The goal is to automatically send out a URL after a client has completed certain milestones. The URL will be part of an email template that will send a link to the site.com hosted version of the VF page with the clients account ID in the URL.. The client will complete the survey and upon clicking save, the survey will be inserted into the clients object by the extension which will draw the account ID from the URL....It all works fine in sandbox, testing just fails as I do not know how to test for AccountID when it gets it from the URL. These are my three codes:
VF page:
<apex:page standardController="Destiny_Survey__c" extensions="extDestinySurvey" >
<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-1.9.1.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-ui-1.10.3.custom.min.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.jQuery, '/css/ui-lightness/jquery-ui-1.10.3.custom.css')}"/>
<apex:form id="theForm">
<apex:pageBlock id="theBlock">
<apex:pageBlockSection id="theSection1" title="Thank you for completing our Destiny Survey {!Destiny_Survey__c.Account__c}" />
<apex:pageBlockSection id="theSection" columns="1" >
<apex:inputField value="{!Destiny_Survey__c.Which_Product_or_Service__c}" label="Which Destiny Product or Service have you just completed?" id="value3"/>
<div id="slider" style="length:50"/>
<apex:inputField value="{!Destiny_Survey__c.How_likely_are_you_to_refer_Destiny__c}" label="How likely is it that you would recommend us to a friend or colleague?" id="value"/>
<apex:inputField value="{!Destiny_Survey__c.Explain_why_you_gave_your_rating__c}" label="Explain why you gave your rating." id="value2"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value="Save" action="{!saveDestinySurvey}"/>
</apex:form>
<script>
function getCleanName(theName)
{
return '#' + theName.replace(/:/gi,"\\:");
}
var valueField = getCleanName( '{!$Component.theForm.theBlock.theSection.value}' );
$j = jQuery.noConflict();
$j( "#slider" ).slider( {enable: true, min: 0, max: 10, orientation: "horizontal", value: 0} );
$j( "#slider" ).on( "slidechange",
function( event, ui )
{
var value = $j( "#slider" ).slider( "option", "value" );
$j( valueField ).val( value );
}
);
</script>
</apex:page>
This is my Extension:
public class extDestinySurvey
{
public Destiny_Survey__c Dess {get;set;}
private Id AccountId
{
get
{
if ( AccountId == null )
{
String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
try
{
if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
}
catch ( Exception e ) {}
}
return AccountId;
}
private set;
}
public extDestinySurvey(ApexPages.StandardController controller)
{
Dess = (Destiny_Survey__c)controller.getRecord();
}
public PageReference saveDestinySurvey()
{
Dess.Account__c = AccountId;
upsert Dess;
// Send the user to the detail page for the new account.
return new PageReference('/apex/DestinyAccount?id=' + AccountId + '&Sfdc.override=1');
}
}
This is my Test that fails at 90%:
@isTest
private class TestExtDestinySurvey {
static testMethod void TestExtDestinySurvey(){
//Testing Survey extension
// create a test account that will be use to reference the Destiny Survey Record
Account acc = new Account(Name = 'Test Account');
insert acc;
//Now lets create Fact Finder record that will be reference for the Standard Account
Destiny_Survey__c DessTest = new Destiny_Survey__c(Account__c = acc.id, Explain_why_you_gave_your_rating__c = 'Because', How_likely_are_you_to_refer_Destiny__c = 8);
//call the apepages stad controller
Apexpages.Standardcontroller stdDess = new Apexpages.Standardcontroller(DessTest);
//now call the class and reference the standardcontroller in the class.
extDestinySurvey ext = new extDestinySurvey(stdDess);
//call the pageReference in the class.
ext.saveDestinySurvey();
}
}
Basically, I have a VF page which is for a survey object that I am creating. The goal is to automatically send out a URL after a client has completed certain milestones. The URL will be part of an email template that will send a link to the site.com hosted version of the VF page with the clients account ID in the URL.. The client will complete the survey and upon clicking save, the survey will be inserted into the clients object by the extension which will draw the account ID from the URL....It all works fine in sandbox, testing just fails as I do not know how to test for AccountID when it gets it from the URL. These are my three codes:
VF page:
<apex:page standardController="Destiny_Survey__c" extensions="extDestinySurvey" >
<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-1.9.1.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-ui-1.10.3.custom.min.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.jQuery, '/css/ui-lightness/jquery-ui-1.10.3.custom.css')}"/>
<apex:form id="theForm">
<apex:pageBlock id="theBlock">
<apex:pageBlockSection id="theSection1" title="Thank you for completing our Destiny Survey {!Destiny_Survey__c.Account__c}" />
<apex:pageBlockSection id="theSection" columns="1" >
<apex:inputField value="{!Destiny_Survey__c.Which_Product_or_Service__c}" label="Which Destiny Product or Service have you just completed?" id="value3"/>
<div id="slider" style="length:50"/>
<apex:inputField value="{!Destiny_Survey__c.How_likely_are_you_to_refer_Destiny__c}" label="How likely is it that you would recommend us to a friend or colleague?" id="value"/>
<apex:inputField value="{!Destiny_Survey__c.Explain_why_you_gave_your_rating__c}" label="Explain why you gave your rating." id="value2"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value="Save" action="{!saveDestinySurvey}"/>
</apex:form>
<script>
function getCleanName(theName)
{
return '#' + theName.replace(/:/gi,"\\:");
}
var valueField = getCleanName( '{!$Component.theForm.theBlock.theSection.value}' );
$j = jQuery.noConflict();
$j( "#slider" ).slider( {enable: true, min: 0, max: 10, orientation: "horizontal", value: 0} );
$j( "#slider" ).on( "slidechange",
function( event, ui )
{
var value = $j( "#slider" ).slider( "option", "value" );
$j( valueField ).val( value );
}
);
</script>
</apex:page>
This is my Extension:
public class extDestinySurvey
{
public Destiny_Survey__c Dess {get;set;}
private Id AccountId
{
get
{
if ( AccountId == null )
{
String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
try
{
if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
}
catch ( Exception e ) {}
}
return AccountId;
}
private set;
}
public extDestinySurvey(ApexPages.StandardController controller)
{
Dess = (Destiny_Survey__c)controller.getRecord();
}
public PageReference saveDestinySurvey()
{
Dess.Account__c = AccountId;
upsert Dess;
// Send the user to the detail page for the new account.
return new PageReference('/apex/DestinyAccount?id=' + AccountId + '&Sfdc.override=1');
}
}
This is my Test that fails at 90%:
@isTest
private class TestExtDestinySurvey {
static testMethod void TestExtDestinySurvey(){
//Testing Survey extension
// create a test account that will be use to reference the Destiny Survey Record
Account acc = new Account(Name = 'Test Account');
insert acc;
//Now lets create Fact Finder record that will be reference for the Standard Account
Destiny_Survey__c DessTest = new Destiny_Survey__c(Account__c = acc.id, Explain_why_you_gave_your_rating__c = 'Because', How_likely_are_you_to_refer_Destiny__c = 8);
//call the apepages stad controller
Apexpages.Standardcontroller stdDess = new Apexpages.Standardcontroller(DessTest);
//now call the class and reference the standardcontroller in the class.
extDestinySurvey ext = new extDestinySurvey(stdDess);
//call the pageReference in the class.
ext.saveDestinySurvey();
}
}
All Answers
Stack Trace Class.extDestinySurvey.saveDestinySurvey: line 31, column 1
Class.TestExtDestinySurvey.TestExtDestinySurvey: line 28, column 1
This is the error
That is not creating an account, maybe another field is required or you have a validation rule.