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 = 'chatterfreeuser101@testorg.com',
                           emailencodingkey = 'UTF-8',
                           lastname = 'Testing',
                           companyname = 'testorg',
                           languagelocalekey = 'en_US',
                           localesidkey = 'en_US',
                           profileId = p.Id,
                           timezonesidkey = 'America/Los_Angeles',
                           username = 'chatterfreeuser101@testorg.com');
        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');

}
4

1 回答 1

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 = 'chatterfreeuser101@testorg.com',
                           emailencodingkey = 'UTF-8',
                           lastname = 'Testing',
                           companyname = 'testorg',
                           languagelocalekey = 'en_US',
                           localesidkey = 'en_US',
                           profileId = p.Id,
                           timezonesidkey = 'America/Los_Angeles',
                           username = 'chatterfreeuser101@testorg.com');
        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 = 'chatterfreeuser101@testorg.com',
                           emailencodingkey = 'UTF-8',
                           lastname = 'Testing',
                           companyname = 'testorg',
                           languagelocalekey = 'en_US',
                           localesidkey = 'en_US',
                           profileId = p.Id,
                           timezonesidkey = 'America/Los_Angeles',
                           username = 'chatterfreeuser101@testorg.com');
        insert u1;
        // perform some assertion regarding expected state of user after insert

        // change something on user
        u1.email = 'test.chatter.free@gmail.com';
        update u1;

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


    }

}
于 2013-07-30T03:07:11.037 回答