0

编写 testMethod 时,我无法检索相关的联系人字段。在下面的示例中,我希望最终断言返回 true。我正在测试的代码工作正常,选择仅在测试上下文中失败。

为什么 SOQL 查询没有返回联系信息?

static testMethod void FailTest()
{
        Contact client = new Contact(FirstName='TestFirst1', LastName='TestFirst', BirthDate = Date.parse('01/01/1986'), Gender__c = 'Male');
        insert client;
        Opportunity opp = new Opportunity(Name = 'Test Opp' + Math.random(), CloseDate=Date.Today(), StageName='ASR - Case Design', Product_Types__c='UL', Face_Amount_Applied_For__c=500, Estimated_Target_Premium__c=1000, X1st_Client__r = client);
        insert opp;

        Opportunity selectOpp = [Select o.X1st_Client__r.LastName From Opportunity o WHERE o.Id = :opp.Id LIMIT 1];

        system.assertNotEquals(opp.X1st_Client__r.LastName, null); //true
        system.assertNotEquals(opp.Name, null); //true
        system.assertNotEquals(selectOpp.Name, null); //true
        system.assertNotEquals(selectOpp.X1st_Client__r.LastName, null); //false, should be true
}
4

1 回答 1

2

我认为问题可能在于您将机会与客户记录相关联的方式。你有 X1st_Client__r = client,我认为你可能真正想做的是 X1st_Client__c = client.Id。我认为插入 DML 不会考虑 X1st_Client__r 中表示的对象。我认为它只会在插入期间关注 X1st_Client__c 值。

于 2012-05-25T18:47:40.393 回答