1

Can you please help me to write a test class for the blow code:

public class retrieveOpenSafetyObservations    {      
   private ApexPages.StandardSetController controller;      
   public List<Safety_Observation__c> getOutbaseSafetyObservations()                      {                                    
 return [SELECT Id, Name, Title__c, Priority__c,   Risk_Factor_Ranking__c,Contractor__c,Status__c          
    FROM Safety_Observation__c WHERE Status__c = 'Open' order by id desc limit 10];      
 }}

here is what I have so far:

 @isTest
        public class testRetrieveOpenSafetyObservation{
            static testMethod void testOpenSafetyOb(){
                    retrieveOpenSafetyObservations oso = new retrieveOpenSafetyObservations(
                      Safety_Observation__c SafObse = new Safety_Observation__c(
                      Basic_Risk_Factors__c ='Design',
                      Cause_Category__c = '1 Equipment and Material problems',
                      Cause_Code_Sub_Category__c = '3E other Human Error not specified here',
                      Contractor__c = 'a0fg0000000u09l',
                      Contributing_Factors__c = 'qwert',
                      Customer_Name__c = '001g0000007q9Z9',
                      Description__c  = 'bla bla',
                      Human_Factor_Domain__c = 'Staffing Issue (Workload etc.)',
                      Human_Factors__c = 'Alarms',
                      Performance_Improvment_Factors__c = 'Organisational Change',
                      Priority__c =  'Low',
                      Risk_Factor_Ranking__c = '2',
                      SO_Number_Name__c = 'Test',
                      Status__c = 'Open',
                      Title__c = 'Test'
                      );  
             insert SafObse ; );                  
                }   


         }
3
  • What are you trying to achieve for this? Is the retrieveOpenSafetyObservations class supposed to be a Visualforce controller? Where's the constructor? If you're happy with your code, then you're probably wanting to test that the getOutbaseSafetyObservations method returns one value.
    – Nick C
    Commented Nov 14, 2013 at 13:48
  • OK I have a VF page which use the retrieveOpenSafetyObservations class.
    – Ghasem
    Commented Nov 14, 2013 at 13:53
  • The VF page gets the data from that class and show them in a Dashboard.
    – Ghasem
    Commented Nov 14, 2013 at 13:54

1 Answer 1

1

In your test, you are trying to use retrieveOpenSafetyObservations like a sObject, and are passing named parameters to it. You cannot do that with a method.

Create and insert SafObse before passing it as a parameter to your function, assuming your class even has an constructor.

7
  • Sorry the retrieveOpenSafetyObservations oso = new retrieveOpenSafetyObservations( was not suppose to be there.
    – Ghasem
    Commented Nov 14, 2013 at 14:00
  • Perhaps then consider posting the code you are having issue with then. We cannot help you very effectively if what you give us to work with is incorrect to begin with.
    – ScottW
    Commented Nov 14, 2013 at 14:02
  • The code I have runs without error but obviously it does not have any effect on my class, and there is %0 code coverage.
    – Ghasem
    Commented Nov 14, 2013 at 14:04
  • Sorry just ignor retrieveOpenSafetyObservations oso = new retrieveOpenSafetyObservations(
    – Ghasem
    Commented Nov 14, 2013 at 14:06
  • Two ways in a test your code can be covered. If it apart of a trigger operation due to DML, or if you just call the code directly. Just instantiate the class from within the test. If you need your page loaded too for the test then do PageReference pr = Page.YourCustomPage; Test.setCurrentPage(pr); but it appears you will need to call your custom class from within your test yourself.
    – ScottW
    Commented Nov 14, 2013 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .