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
MATTYBMEMATTYBME 

Help with a very small test method pls

Hi I am struggling to get a complete test method to work from the below controller. Any help would be appreciated.

 

public class blogController { public Blog_Post__c[] getPosts() { return [select Name, id, CreatedDate, CreatedBy.Name, Post__c from Blog_Post__c]; } public PageReference newPost() { PageReference pageRef= new PageReference('/apex/blogedit'); pageRef.setredirect(true); return pageRef; } public static testMethod void myTest(){ Blog_Post__c testBlog = new Blog_Post__c(name='test blog name'); insert testBlog; } }

 

 

 

XactiumBenXactiumBen

Your test method doesn't actually call any of the code in your controller.  You actually need to create a new instance of your controller within your test method:

 

public static testMethod void myTest(){ Blog_Post__c testBlog = new Blog_Post__c(name='test blog name'); insert testBlog; blogController controller = new blogController(); controller.getPosts(); ... }

 

MATTYBMEMATTYBME

Still requiring a little more help please. I am just not a coder to be honest.

 

 

public class blogController { public Blog_Post__c[] getPosts() { return [select Name, id, CreatedDate, CreatedBy.Name, Post__c from Blog_Post__c]; } public PageReference newPost() { PageReference pageRef= new PageReference('/apex/blogedit'); pageRef.setredirect(true); return pageRef; } public static testMethod void myTest(){ Blog_Post__c testBlog = new Blog_Post__c(name='test blog name'); insert testBlog; blogController controller = new blogController(); controller.getPosts(); return [select Name, id, CreatedDate, CreatedBy.Name, Post__c from Blog_Post__c]; } public PageReference newPost() { PageReference pageRef= new PageReference('/apex/blogedit'); pageRef.setredirect(true); return pageRef; } }

 

 

 

mtbclimbermtbclimber

I don't think your code compiles does it?

 

XactiumBen has given you a great starting point for this test. You should be able to round it out pretty easily, in fact from what I can tell you just need to remove code to make this work. You'll want to add a test that calls the newPost() method on the controller instance just like the getPosts() one stubbed out by XactiumBen.

 

So far your use cases seem pretty generic so another approach you could go with this is to try to drop Apex from the equation altogether.  From what I see below you can use the Standard List Controller to get the collection of blog posts and you can create a link in your page using the $Page global to achieve the functionality expressed below without writing any Apex.  Both are documented in the Visualforce developer guide.

Message Edited by mtbclimber on 12-21-2009 11:02 AM