4

システム管理者プロファイルを使用して、テストクラスにユーザーを作成したいと思います。

私が抱えている問題は、seeAllData = falseであり、テストクラスではテストクラス内のデータのみを使用するということです。

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

これは値を返さない可能性があります。

ユーザーを作成する方法についてのアイデア

編集

seeAllData = falseでも、クエリのデータを取得できます。それで、SeeAllData = falseであっても、プロファイルレコードが表示されますか?

4

2 に答える 2

7

ドキュメントによると、User オブジェクトと Profile オブジェクトは「組織の管理に使用されるオブジェクト」と見なされ、テスト クラス/メソッドにIsTest(SeeAllData=true)アノテーションが含まれているかどうかに関係なくアクセスできます。

この方法でアクセスできるその他のオブジェクトには、次のものがあります。

  • 組織
  • レコードの種類
  • Apexクラス
  • ApexTrigger
  • ApexComponent
  • Apexページ
于 2012-07-25T12:14:47.150 に答える
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); 

} 
于 2018-01-02T11:36:28.940 に答える