Skip to content

Instantly share code, notes, and snippets.

@pchittum
Created September 25, 2014 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchittum/bfad7380b97f33c43b2e to your computer and use it in GitHub Desktop.
Save pchittum/bfad7380b97f33c43b2e to your computer and use it in GitHub Desktop.
SOSL and Testing for SOSL in Apex
public PageReference doSearch() {
results = (List<Candidate__c>)[FIND :searchText IN ALL FIELDS RETURNING Candidate__c(Id, First_Name__c, Last_Name__c, Email__c, City__c, State_Province__c, Phone__c)] [0];
return null;
}
Candidate__c cand = new Candidate__c(first_name__c = 'Sammy', last_name__c = 'Bakersfield', email__c ='s@bakersfield.com');
insert cand;
// the following 3 lines are special; for testing SOSL in testMethods
// see the Apex PDF for more info on SOSL in testMethods
Id [] fixedSearchResults = new Id[] {cand.id};
Test.setFixedSearchResults(fixedSearchResults);
// now we do the controller's sosl search
extension.doSearch();
// verify the results of the search
List<Candidate__c> results = extension.results;
System.debug('results=' + results);
System.assert(results.size() > 0);
System.assertNotEquals(results[0].email__c,null);
System.assertEquals(results[0].last_name__c,cand.last_name__c);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment