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

如何在shiny的testServer测试中检测子模块server是否被调用

方案1:测试侧包装子模块标记调用状态

该方案完全不侵入原有业务代码,仅在测试逻辑中给child_srv加一层包装记录调用状态即可:

# 测试侧新增调用标记逻辑,不改动原有子模块、父模块、call_child代码
called_flag <- FALSE
wrapped_child_srv <- function(id) {
  called_flag <<- TRUE
  child_srv(id)
}

testServer(
  app = parent_srv,
  args = list(module = wrapped_child_srv), 
  expr = {
    tinytest::expect_true(called_flag)
    tinytest::expect_null(call_child(module = module))
})

方案2:通过session对象直接验证子模块注册状态

如果你完全不想修改传入的模块参数,可直接利用Shiny模块调用后会自动注册输入输出的特性,从session中读取验证:

testServer(
  app = parent_srv,
  args = list(module = child_srv), 
  expr = {
    # 子模块id为parent,注册的输出id为out,拼接后在父session中的输出id为parent-out
    tinytest::expect_true("parent-out" %in% ls(session$output))
    # 还可进一步验证子模块逻辑正常
    session$setInputs(`parent-select` = "b")
    tinytest::expect_equal(session$getOutput("parent-out"), "b")
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 08:24:03