如何在 salesforce 中对 "lastmodified" 日期执行 class 覆盖率测试?
How to perform the test class coverage for "lastmodified" date in salesforce?
我正在尝试测试 class 系统保护字段的覆盖率以测试代码覆盖率。
销售人员提供了解决方案 here。
不允许我设置动态日期,因此我也有触发器,即使我使用 test.setStartDate()
修改日期正在生效。
Here 很有帮助link 可能对您有用
以下代码用于设置系统保护字段测试代码覆盖率,
正如我们从这里知道的那样,我们可以设置创建日期字段,那么最后修改日期呢?
答案来了:
用例是如果我们在上次修改日期使用带有过滤条件的 SOQL,例如
SELECT Id,Name FROM Lead WHERE DAY_ONLY(lastmodifieddate)=today and
HOUR_IN_DAY(lastmodifieddate)>4 and HOUR_IN_DAY(lastmodifieddate)<15
我们可以看到我正在查询今天和4到15(格林威治标准时间)之间的时间修改的记录,我需要今天和特定时间段的动态测试数据。
那么如何覆盖这个,bam使用往返序列化。
Lead l= new Lead();
l.lastName='test';
l.Status='New';
l.Company='test';
Date myDate = Date.today();
Time myTime = Time.newInstance(8, 0, 0, 0);
DateTime dt1 = DateTime.newInstanceGMT(myDate, myTime);
l.CreatedDate=dt1;
l.LastModifiedDate=dt1;
//I am serializing the sobject to string.
string leadJSON=JSON.serialize(l);
//I am De-serializing the string back to sobject
Lead ll = (Lead) JSON.deserialize(leadJSON, Lead.class );
//In this way it will accept the dynamic dates we specify for system date fields.
insert ll;
//Remember this is for test class purpose only.
我正在尝试测试 class 系统保护字段的覆盖率以测试代码覆盖率。
销售人员提供了解决方案 here。
不允许我设置动态日期,因此我也有触发器,即使我使用 test.setStartDate()
修改日期正在生效。
Here 很有帮助link 可能对您有用
以下代码用于设置系统保护字段测试代码覆盖率, 正如我们从这里知道的那样,我们可以设置创建日期字段,那么最后修改日期呢?
答案来了: 用例是如果我们在上次修改日期使用带有过滤条件的 SOQL,例如
SELECT Id,Name FROM Lead WHERE DAY_ONLY(lastmodifieddate)=today and
HOUR_IN_DAY(lastmodifieddate)>4 and HOUR_IN_DAY(lastmodifieddate)<15
我们可以看到我正在查询今天和4到15(格林威治标准时间)之间的时间修改的记录,我需要今天和特定时间段的动态测试数据。 那么如何覆盖这个,bam使用往返序列化。
Lead l= new Lead();
l.lastName='test';
l.Status='New';
l.Company='test';
Date myDate = Date.today();
Time myTime = Time.newInstance(8, 0, 0, 0);
DateTime dt1 = DateTime.newInstanceGMT(myDate, myTime);
l.CreatedDate=dt1;
l.LastModifiedDate=dt1;
//I am serializing the sobject to string.
string leadJSON=JSON.serialize(l);
//I am De-serializing the string back to sobject
Lead ll = (Lead) JSON.deserialize(leadJSON, Lead.class );
//In this way it will accept the dynamic dates we specify for system date fields.
insert ll;
//Remember this is for test class purpose only.