You need to sign in to do that
Don't have an account?
jaw99KCG
Test coverage help please, thanks!
Would love some help getting test coverage for the following:
public class accountContactExt { Account a; public accountContactExt(ApexPages.StandardController controller) { a = (Account) controller.getRecord(); } public List<Contact> getSalesContacts() { return [select name, title, email, Client_Role__c, phone from contact where accountid = :a.id and Client_Role__c like '%']; } }
which feeds a nice inline list to the account page via:
<apex:page standardController="Account" extensions="accountContactExt"> <apex:pageBlock title="Client Roles"> <apex:pageBlockTable value="{!salescontacts}" var="c"> <apex:column headerValue="Contact Name"><apex:outputLink value="{!URLFOR($Action.Contact.View,c.id)}" target="_blank">{!c.name}</apex:outputLink></apex:column> <apex:column value="{!c.Client_Role__c}"/> <apex:column value="{!c.title}"/> <apex:column headervalue="Email"><apex:outputField value="{!c.email}"/></apex:column> <apex:column value="{!c.phone}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
I bet simple for experienced coders but Testing is a real hump for me right now - help getting coverage for this would be appreciated, thanks!
awesome.
Needed to add another acct field to validate in prod, but thanks for the help and patience!
All Answers
You need something like this:
Of course, you need to be sure to add data to the required fields for account.
Ah, but do i need to insert contacts, too?
Yes, that way you can assert the results from the getSalesContacts function call.
Otherwise, that might return null and/or fail.
OK, I want to insert a contact, and do I need to have the account id on the contact look up to the account, too?
this is a mess, i can't get past line 4 Error: Compile Error: unexpected token: '=' at line 4 column 17
thanks
This needs to be in a test function, not a class.
Here is what the method should look like:
OK, so that method won't compile on it's own.
As a static method do I put it in the class? Sorry, thanks
This has nothing to do with static, ALL methods must be in a class.
Static just means that it doesn't have polymorphism - the function is the same across all instances of the object. For that reason, you must call it from the base class and not the instance of the class.
Generally, I put the test code for a given class in that class.
Gotcha, sorta. I guess it isn't clear to me then how to fold that test into the class. Or where to put it in and have it like it.
It is just another method in the class. I usually put test methods at the bottom of the class.
So for example, your full class (with all methods including test methods) might look like this:
thanks for so much patience. slowly this is coming back to me.
So we are not invoking it correctly. Ah I can I see where it is looking but it looks like to me the method should be there.
it is a typo on my part, change this line as pasted:
accountContactExt MyController = new accountContactExt(stdController);
Sorry about that :)
Please check out my salesforce blog here.
If you like it and/or my posts helped you, you can always nominate me for MVP here. :smileyvery-happy:
awesome.
Needed to add another acct field to validate in prod, but thanks for the help and patience!