0

我写了一个简单的 SalesForce 触发器。一旦潜在客户变得不合格,我想将 IsUnreadbyOwner 字段更新为 False(这发生在我们的用户将潜在客户留在队列中时)。

我的触发器是:

trigger UnqualifiedLead on Lead (after update) {
    for(Lead lead: Trigger.new)
    {
        if (lead.Status == 'Unqualified')
        {
            lead.IsUnreadByOwner = False;   
        }
    }
}

我的测试类,AFAIK 应该是这样的:

@isTest
private class UnqualifiedLeadTest {
static testMethod void myUnitTest() {
        // Setup the lead record
        Lead lead = new Lead();
        lead.LastName = 'last';
        lead.FirstName = 'First';
        lead.Company = 'Company';
        lead.Status = 'Unqualified';
        lead.IsUnreadByOwner = True;
        insert lead;
    }
}

但是,我收到一个覆盖错误:覆盖率为 0%。

我的错误在哪里?

4

2 回答 2

2

In your test class you are only inserting the record yet your trigger is only setup to capture update events. You will either need to insert the lead then update to execute your trigger or add "on insert" to your trigger so that it runs when a lead is inserted and updated.

Also, you are using an after event when you should be using a before event trigger for this type of update. Saves having to perform an additional DML operation.

于 2012-07-17T13:45:59.040 回答
1

同样重要的是要注意您的单元测试实际上甚至没有测试任何东西。您的代码应如下所示:

trigger UnqualifiedLead on Lead (before update) 
{
    for(Lead lead: Trigger.new)
    {
        if (lead.Status == 'Unqualified')
        {
            lead.IsUnreadByOwner = False;   
        }
    }
}

测试类:

@isTest
private class UnqualifiedLeadTest 
{
    static testMethod void myUnitTest() 
    {
        // Setup the lead record
        Lead lead = new Lead();
        lead.LastName = 'last';
        lead.FirstName = 'First';
        lead.Company = 'Company';
        lead.Status = 'NewStatus';
        lead.IsUnreadByOwner = True;
        insert lead;

        test.startTest();

        lead.Status = 'Unqualified';
        update lead;

        Lead lTest = [SELECT Id, IsUnreadByOwner FROM Lead WHERE Id=:lead.Id];
        system.assertEquals(false, lTest.IsUnreadByOwner);

        test.stopTest();
    }
}
于 2012-07-18T06:04:41.573 回答