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
Developer.mikie.Apex.StudentDeveloper.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();
    }
   
    }


Best Answer chosen by Developer.mikie.Apex.Student
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
No validation rule....I thought it was because the idea behind the Extension is to take the account ID from the URL...But I did not know how to write this into the test.

All Answers

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Error Message System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account__c]: [Account__c]
Stack Trace Class.extDestinySurvey.saveDestinySurvey: line 31, column 1
Class.TestExtDestinySurvey.TestExtDestinySurvey: line 28, column 1




This is the error 
AsiereikiAsiereiki
Account acc = new Account(Name = 'Test Account');
insert acc;

That is not creating an account, maybe another field is required or you have a validation rule.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
No validation rule....I thought it was because the idea behind the Extension is to take the account ID from the URL...But I did not know how to write this into the test.
This was selected as the best answer