2013-07-29 2 views
1

나는 새로운 수다 무료 사용자가 조직 생성 할 수 있도록 등록 처리기에 대한 테스트 클래스를 구축을 위해 노력 초보자입니다. 잡담이없는 사용자를 생성하기 위해 작업 트리거는 DomainList라는 사용자 지정 개체에 저장된 허용 가능한 전자 메일 도메인의 미리 정의 된 목록에 대해 새 사용자의 전자 메일 도메인을 확인합니다.에이펙스 - 테스트 클래스 추가 채팅 무료 사용자는

코드 커버리지를 위해 테스트 클래스를보다 잘 구성하는 방법에 대한 제안을 찾고 있습니다. 어떤 아이디어 감사, 감사! 당신이 정확하지 않기 때문에

global class RegHandlerDomain implements Auth.RegistrationHandler { 
    global User createUser(Id portalId, Auth.UserData data) { 

     String domain = data.email.split('@')[1]; 

     List<DomainList__c> listedDomains = [SELECT id, 
                name 
              FROM DomainList__c 
              WHERE Email_Domain__c = : domain]; 

     if (listedDomains.size() == 1) { 
      User u = new User(); 
      Profile p = [SELECT Id 
         FROM profile WHERE name = 'Chatter Free User']; 

      // Use incoming email for username 
      u.username = data.email; 
      u.email = data.email; 
      u.lastName = data.lastName; 
      u.firstName = data.firstName; 
      u.alias = (data.username != null) ? data.username : data.identifier; 
      if (u.alias.length() > 8) { 
       u.alias = u.alias.substring(0, 8); 
      } 
      u.languagelocalekey = UserInfo.getLocale(); 
      u.localesidkey = UserInfo.getLocale(); 
      u.emailEncodingKey = 'UTF-8'; 
      u.timeZoneSidKey = 'America/Los_Angeles'; 
      u.profileId = p.Id; 

      System.debug('Returning new user record for ' + data.username); 

      return u; 

     } else return null; 

    } 

    global void updateUser(Id userId, Id portalId, Auth.UserData data) { 
     User u = new User(id = userId); 

     u.email = data.email; 
     u.lastName = data.lastName; 
     u.firstName = data.firstName; 

     System.debug('Updating user record for ' + data.username); 

     update(u); 
    } 
} 

테스트 클래스는 삽입이 적절한 단위 테스트를 만들 필요로하는 모든의

@isTest(SeeAllData = true) 
public class RegHandlerTest { 

    public void myTestMethod1() { 
     //CREATE CHATTER FREE TEST USER 
     Profile p = [select id 
        from profile where name = 'Chatter Free User']; 
     User u1 = new User(alias = 'chfree01', 
          email = '[email protected]', 
          emailencodingkey = 'UTF-8', 
          lastname = 'Testing', 
          companyname = 'testorg', 
          languagelocalekey = 'en_US', 
          localesidkey = 'en_US', 
          profileId = p.Id, 
          timezonesidkey = 'America/Los_Angeles', 
          username = '[email protected]'); 
     insert u1; 
    } 

    //CHECK NEW USER EMAIL DOMAIN TO SEE IF IN CUSTOM OBJECT DOMAINLIST 
    List<DomainList__c> listedDomains = [select id, email_domain__c 
             from DomainList__c 
             where name = 'testorg' LIMIT 1]; 
    if (listedDomains.size() == 1) System.debug('WORKS'); 

} 

답변

0

전나무 전에 '}'는 찾고 있습니다.

@isTest(SeeAllData = true) 
public class RegHandlerTest { 

    @isTest private static void verifyInsertChatterFreeUser() { 
     //CREATE CHATTER FREE TEST USER 
     Profile p = [SELECT id 
        FROM profile 
        WHERE name = 'Chatter Free User']; 
     User u1 = new User(alias = 'chfree01', 
          email = '[email protected]', 
          emailencodingkey = 'UTF-8', 
          lastname = 'Testing', 
          companyname = 'testorg', 
          languagelocalekey = 'en_US', 
          localesidkey = 'en_US', 
          profileId = p.Id, 
          timezonesidkey = 'America/Los_Angeles', 
          username = '[email protected]'); 
     insert u1; 
     // perform some assertions regarding expected state of user after insert 

     //CHECK NEW USER EMAIL DOMAIN TO SEE IF IN CUSTOM OBJECT DOMAINLIST 
     List<DomainList__c> listedDomains = [SELECT id, email_domain__c 
              FROM DomainList__c 
              WHERE name = 'testorg' LIMIT 1]; 
     // as you've annotated class with SeeAllData=true, this assertion always will be true 
     System.assertEquals(1, listedDomains.size()); 
    } 

    @isTest private static void verifyUpdateChatterFreeUser() { 
     //CREATE CHATTER FREE TEST USER 
     Profile p = [SELECT id 
        FROM profile 
        WHERE name = 'Chatter Free User']; 
     User u1 = new User(alias = 'chfree01', 
          email = '[email protected]', 
          emailencodingkey = 'UTF-8', 
          lastname = 'Testing', 
          companyname = 'testorg', 
          languagelocalekey = 'en_US', 
          localesidkey = 'en_US', 
          profileId = p.Id, 
          timezonesidkey = 'America/Los_Angeles', 
          username = '[email protected]'); 
     insert u1; 
     // perform some assertion regarding expected state of user after insert 

     // change something on user 
     u1.email = '[email protected]'; 
     update u1; 

     // perform some assertions regarding expected state of user after update 
     // System.assertEquals('expected value', u1.field); 


    } 

} 
+0

파벨, 올바른 방향으로 나를 조종 해 주셔서 감사합니다! – Matthew