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
BRupholdtBRupholdt 

Beginner's Unit Testing

After searching the forums and reading many of the resources posted, I still don't understand.
 
I've made a very simple VF page with controller that needs unit testing so I can publish it to my production org.  I've started extreme basic so that I can understand the entire process.  Try as I might, I don't understand how to make the unit tests for these.  I learn best by seeing it done but the examples I find are far more complex than I can wrap my head around just yet.  Would someone be willing to write unit tests for these two items so that I have a working example to go forward with?  I would greatly appreciate it.
 
Code:
public String getLongDate() {
  Datetime cDT = System.now();
  return cDT.format('EEEE, MMMM d, yyyy');
}

public Goals__c[] getGoals() {
  return [
    Select Completion_Date__c, CreatedById, CreatedBy.Alias, CreatedBy.Id, CreatedDate, 
      Goal__c, Id, IsDeleted, Name, OwnerId, Status__c, Target_Date__c, Timeliness__c 
    from Goals__c
    where Completion_Date__c = null
    order by Target_Date__c desc
  ];
}

I piece this test together from what I've found but it seems not to do anything toward my unit testing requirements.  At least it doesn't error like everything else I've tried to do :/
 
Code:
static testMethod void myTest() {
  Goals__c g = new Goals__c(Goal__c='test');
  insert g;
  g = [Select Goal__c from Goals__c where Id = :g.Id];
  System.assertEquals( 'test', g.Goal__c );
}

 

 
 
BritishBoyinDCBritishBoyinDC
Well, to really test it, you need to be testing the results that are brought back (i.e. does the where clause produce the right results)

But to get started, the test method needs to at least test that the controller class works, so you need to call getGoals()..

public static testMethod void myTest() {

// Not sure what the controller class is called, but if we assume it is called myTest
myTest myTest = new myTest();

myTest.getGoals();

// you should then do an assert to check that the correct number of results are being returned etc...

}
BRupholdtBRupholdt
Ahh!  That helps so much!  I missed that the controller name was what was being used to call the tests.  (Yeah, I'm a newb's newb.)  It's obvious, I just hadn't put it together.  I learn better by doing than by reading.
 
I  think I can figure it out now.  Thank you.