I am having trouble working out how to get my SOSL search page to test with a solid percentage. The reason I am using SOSL is because of the Long Text Areas that I am searching through.
Here is my Controller:
Public with sharing class SOSLController{
Public List<Journal_Entry__c> entList {get;set;}
Public String searchStr{get;set;}
Public SOSLController(){
}
Public void soslDemo_method(){
String userID = UserInfo.getUserId();
entList = New List<Journal_Entry__c>();
if(searchStr.length() > 1){
String searchStr1 = '*'+searchStr+'*';
String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING
Journal_Entry__c (Id,Name,DateText__c,Submitted_for_Interp_Count__c,Created_Self_Interp__c,Haven__c,UserID__c
WHERE UserID__c LIKE :userID)';
List<List <sObject>> searchList = search.query(searchQuery);
entList = ((List<Journal_Entry__c>)searchList[0]);
if(entList.size() == 0){
apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..'));
return;
}
}
else{
apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least two characters..'));
return;
}
}
}
And this is what I have so far with my Test... You will notice that I give the userID string a user Id that works in my VF pages for the test I'm trying to do, but in the Controller, this Id is captured from the active user.
@isTest
public class TestSOSLController{
public static testMethod void testSoslFixedResults() {
PageReference myVfPage = Page.MyJournal;
Test.setCurrentPage(myVfPage);
SOSLController testCont = new SOSLController();
String userID = '00555000001R62HAAS';
Id [] fixedSearchResults= new Id[1];
fixedSearchResults[0] = 'a0955000001ODaU';
Test.setFixedSearchResults(fixedSearchResults);
List<List<SObject>> searchList = [FIND 'test'
IN ALL FIELDS RETURNING
Journal_Entry__c(Id,Name,DateText__c,Submitted_for_Interp_Count__c,Created_Self_Interp__c,Haven__c,UserID__c
WHERE UserID__c LIKE :userID LIMIT 1)];
system.assert(searchList != null);
}
}
I understand that SOSL has a special syntax to be used when Testing, based on this page "Adding SOSL Queries to Unit Tests" and you will see that I have attempted to use this, but I am obviously failing misserably.
I would greatly appreciate any help I can get!