Salesforce Apex:测试未进行标注 [英] Salesforce Apex: test that callout hasn't been made
问题描述
我想编写一个单元测试来检查是否没有从触发器发出标注.
我知道如何通过实现 HttpCalloutMock 来测试标注是否正确:
I want to write a unit test that checks that callout hasn't been made from the trigger.
I know how to test if the callout is made correctly - by implementing HttpCalloutMock:
global class MyHttpCalloutMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
//test HTTPRequest here
}
}
但是如果没有发出 HTTP 请求,那么 response() 方法将不会被调用.因此,这种方法根本不会测试是否发出了请求.
我需要这样的东西:
But if no HTTP request is made, then the respond() method won't be called. So this approach doesn't test if the request was made at all.
I need something like this:
HTTPRequest.assertNoRequestsHaveBeenMade();
我该怎么做?
推荐答案
所以我想通了.结果证明 Salesforce 有方法 Test.startTest()
和 Test.stopTest()
使异步标注同步:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
So I figured it out. It turns out that Salesforce has methods Test.startTest()
and Test.stopTest()
that make asynchronous callouts synchronous: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
使标注同步后,测试它们就容易多了.
After making callouts synchronous it's much easier to test them.
这篇关于Salesforce Apex:测试未进行标注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!