You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ballerina中结束Mock后恢复被Stub的模拟函数?

恢复Ballerina中被Mock的原函数

要恢复被test:MockFunction模拟的原函数,你可以使用Ballerina测试模块提供的test:reset()方法,它会清除对Mock对象的配置,让后续调用回归原函数逻辑。

修改后的代码示例

import ballerina/test;

@test:Mock {
    functionName: "getDatabaseCredentials"
}
test:MockFunction mockDbCredFn = new();

function mockGetDatabaseCredentials() returns string {
    return "postgresql://username:password@hostname:port/database";
}

@test:Config
function test1() {
    test:when(mockDbCredFn).call("mockGetDatabaseCredentials");
    test:assertEquals(getDatabaseCredentials(), "postgresql://username:password@hostname:port/database");
    
    // 调用reset恢复原函数
    test:reset(mockDbCredFn);
    
    // 后续调用getDatabaseCredentials()会执行原函数逻辑
}

补充说明

  • test:reset()方法接收你创建的MockFunction实例作为参数,执行后该Mock的所有配置都会被清除
  • 如果需要批量恢复所有Mock,也可以使用不带参数的test:reset(),它会重置当前测试中的所有Mock对象

内容的提问来源于stack exchange,提问作者Gayal Dassanayake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 03:59:55