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
AkiTAkiT 

Tests results "REQUIRED_FIELD_MISSING"

Hi,

I am trying to finsh test method for my controller but something is wrong.. I get "REQUIRED_FIELD_MISSING" on my upsert call.

I have created successfully test data and I am adding the id parameter to the page. Any ideas? below code...

Code:
public static testMethod void testUserPage() {
 
 
 User_Request__c u = UserRequest.setupTestReq();
 

    // Construct the standard controller for user request.
    ApexPages.StandardController s = new ApexPages.standardController(new User_Request__c());
    
    // Switch to runtime context 
    Test.startTest();
    
    // Construct the user req class
    UserRequest ux = new UserRequest(s);

   // Call save on the ux
    PageReference result = ux.save();

    // Switch back to test context
    Test.stopTest();
    
    // Verify the navigation outcome is as expected
    System.assertEquals(result.getUrl(), s.view().getUrl());
    

}

// Create test user request
 private static User_Request__c setupTestReq() {
 
  Id rid = [select Id from UserRole limit 1].Id;
  Id pid = [select Id from Profile limit 1].Id;
  
  User_Request__c u = new User_Request__c(
   
   First_Name__c = 'Dummy',
   Last_Name__c = 'Test',
   Role_ID__c = rid,
   ProfileID__c = pid,
   User_Name__c = 'dummy@test.com',
   Title__c = 'Sales',
   Legacy_User_ID__c = 'dummy',
   Language__c = 'Finnish',
   Country__c = 'Finland'
  
  );
  
  insert u;
  system.debug('ID is: '+u.id);
  
     PageReference pref = Page.UserRequest;
     pref.getParameters().put('id',u.id);
     Test.setCurrentPage(pref);
     
     return u;
     
 }

 

JimRaeJimRae
The error message should tell you which field is missing, If I remember correctly, it is displayed in brackets.
AkiTAkiT
Error message says that First_Name__c is required.. but that is not the real issue.

I get the error when I call save method in test:
// Call save on the ux
PageReference result = ux.save();


In save method I do upsert. I thought that the above is testing with the test data I created and
I pass the id param to the pagereference:


PageReference pref = Page.UserRequest;
pref.getParameters().put('id',u.id);
Test.setCurrentPage(pref);

Sadly I didn't manage to find any doc on this...





JimRaeJimRae
Does your page use a standard controller, or are you using a custom controller? That might be your issue.
AkiTAkiT
Page uses standardcontroller and extension.

The save method is written in the extension. I was trying to get hint from pdf2Quote wiki example - maybe the issue is there as the example uses only custom controller...
AkiTAkiT
Found the issue... In

// Construct the standard controller for user request.
ApexPages.StandardController s = new ApexPages.standardController(new User_Request__c());

I needed to pass the existing test record as an argument for the controller.