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
Yosef KaptaYosef Kapta 

Replicate user logging in via Salesforce 1 Mobile App in Apex Test Class

I've created a trigger that checks the LoginHistory object for ''Salesforce1 for iOS'' to enforce an Apex validation rule. The short version is that State and Country picklists are not supported by SF1, however we want to make Country and City required, but users still want to be able to create accounts via SF1, hence the trigger.

Any ideas on what my test logic should be? Obviously I would need to trigger it by inserting the code, but how do I add the user element to the logic? Unfortuantely LoginHistory.Application is not a writeable field, therefore I can't hardcode a value - I would need to insert an object in order for this field to be returned, what object should I be looking for? 

Thank you for your help.
KevinPKevinP
You may have found one of those situations where there's no good way to test this. Only a list of bad ways and a sort of "pick your poison" approach. 

One way i've solved similar issues in the past was a 1-2 punch of Static variable in a utility class, and some additional logic within the class being tested itself. something like this:
 
public with sharing class UglyHackVariable {

  public static boolean isUglyHackGoingOn = false;

}

Then in your actual class where you're looking to see if something has been logged you can extend your IF check to be something like this:
 
//blah blah class header

if(loginhistory.application = 'SF1' || uglyHackVariable.isUglyHackGoingOn){
//do awesome stuff
}

and finally in your test:
 
@isTest
private static doesAnyoneActuallyReadWhatINameMyClassesHere {

  private void testMethod testOfDoom() {
    // the naming of this should clue future developers into the fact that an
    // UGLY HACK IS GOING ON HERE
    UglyHackVariable.isUglyHackGoingOn = true;

    //do your test stuff.

}