You need to sign in to do that
Don't have an account?
Gdev20000
Help with test class 0% covered
It's almost my first test class, please anyone can help me.
I get 0% covered by test class for my custom controller:
My class:
public with sharing class MyCustomLookupController { public Influencer__c Influencer { get; set; } public Id accountplanid { get; set; } public MyCustomLookupController () { accountplanid = System.currentPageReference().getParameters().get('id'); Influencer = new Influencer__c(Account_Plan__c = accountplanid ); } public PageReference save() { Contact c = [SELECT Job_title__c,name FROM Contact WHERE Id = :Influencer.Contact__c]; Influencer.Title__c = c.Job_title__c; influencer.name=c.name; insert Influencer; PageReference acctPage = new PageReference('/' + Influencer.id); acctPage.setRedirect(true); return acctPage; } public PageReference cancel() { PageReference aPage = new PageReference('/' + accountplanid); aPage.setRedirect(true); return aPage; } }
My test class:
@isTest private class MyCustomLookupControllerTestClass { static testMethod void validate() { PageReference pageRef = Page.MyCustomLookup; Test.setCurrentPage(pageRef); // Create a new account object. Account testAccount = new Account(Name = 'TestAccount'); testAccount.Red_Account_End_Date__c=date.today(); testAccount.Red_Account_Reason__c='reason'; testAccount.Red_Account__c= true; insert testAccount; //CREATE TEST ACCOUNT PLAN Account_Plan__c accountplan=new Account_Plan__c (); accountplan.account__c=testaccount.id; insert accountplan; //CREATE TEST CONTACT Contact contact = new contact(lastname='lname'); contact.Job_title__c='salesrep'; insert contact; Contact contact2 = new contact(lastname='lname'); insert contact2; //CREATE TEST INFLUENCER Influencer__C influencertest = new Influencer__c(); influencertest.Account_Plan__c= accountplan.id; Influencertest.Title__c = contact.Job_title__c; influencertest.contact__c=contact2.id; insert influencertest; } }
Sorry, I have to learn a lots of things.
Thank you,Br.
You need to initiate the controller.
Try This:
Thank you so much, you are very kind.
Now i'm getting this error:
please can you have a look.
Thanks again.
I am seeing a problem with your controller class
(You never queried for Contact__c and you were using the same in save method)
No,it isn't a problem.
I'm creating a new influencer's record in the controller getting the id from the url.
I query for the contact with:
Thanks for your reply.
Your select is not returning any results.
Why:
When you initiate the controller in the test it creating a new influncer and not utilizing the one in your test code. In the one you are creating in your conroller it does not have a contact associated to it.
The actual code running in your test is this:
Try this before controller.save(); :
This should override the new influencer with the one your test created and the save correctly.
Thanks jhugger i have updated my code but now i get this error:
the line interessed is that with controller.save();