Salesforce Apex:测试尚未进行标注 [英] Salesforce Apex: test that callout hasn't been made

查看:143
本文介绍了Salesforce Apex:测试尚未进行标注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个单元测试,以检查是否未从触发器进行标注.

我知道如何测试标注是否正确-通过实现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():

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆