需求:为RemoteAction写TestClass
如果有如下的RemoteAction方法
@RemoteAction
public static List<Map<String, String>> searchUsers(String term) {
List<Map<String, String>> results = new List<Map<String, String>>();
if (String.isEmpty(term)) {
return results;
}
String searchTerm = '%' + term + '%';
system.debug(term);
List<User> users = [SELECT Id, Name FROM User WHERE Name LIKE :searchTerm LIMIT 10];
for (User user : users) {
Map<String, String> userData = new Map<String, String>{
'value' => user.Name,
'label' => user.Name
};
results.add(userData);
}
return results;
}
TestClass的写法
@isTest
static void testSearchUsers()
{
Profile p = [SELECT Id FROM Profile WHERE Name='システム管理者' limit 1];
User userItem = new User(Alias = 'st', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='ABC', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id ,
TimeZoneSidKey='America/Los_Angeles', Username='systemadTest001@testorg.com',
CommunityNickname = 'Test');
insert userItem;
Test.startTest();
PostingMapController.searchUsers(userItem.LastName);
Test.stopTest();
//当然了,需要在最后写上System.assertEquals();来验证执行结果是否正确
}