2012-07-25 71 views
2

我想使用系统管理员配置文件在测试类中创建用户。如何在测试类中的seeAlldata = false时使用系统管理员配置文件创建用户

我有的问题是,我seeAllData = false在测试类中只会使用测试类内的数据。

List<Profile> ps = [select id, name from Profile where name = 'System Administrator']; 

这可能不会返回任何值。

我们如何能够创建一个用户

编辑

我可以得到的数据查询上即使有seeAllData =虚假的任何想法。那么即使SeeAllData = false,Profile记录也是可见的吗?

回答

5

根据documentation,User和Profile对象被视为“用于管理组织的对象”,无论您的测试类/方法是否包含IsTest(SeeAllData=true)注释,都可以访问它们。

是用这种方式访问​​

的对象包括:

  • 组织
  • 记录类型
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage
0
We can use the code directly to create a role and attach it to a user with system admin profile. 

@isTest static void UserCreate() { 
UserRole obj=new UserRole(Name= 'ABC'); 
insert obj; 

Profile pf= [Select Id from profile where Name='System Administrator']; 

String orgId=UserInfo.getOrganizationId(); 
String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') 

Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
String uniqueName=orgId+dateString+RandomId; 

User uu=new User(firstname = 'Alan', 
lastName = 'McCarthy', 
email = uniqueName + '@test' + orgId + '.org', 
Username = uniqueName + '@test' + orgId + '.org', 
EmailEncodingKey = 'ISO-8859-1', 
Alias = uniqueName.substring(18, 23), 
TimeZoneSidKey = 'America/Los_Angeles', 
LocaleSidKey = 'en_US', 
LanguageLocaleKey = 'en_US', 
ProfileId = pf.Id, 
UserRoleId = obj.Id); 

} 
相关问题