测试中的闪亮反应上下文

时间:2020-10-27 08:23:30

标签: r shiny testthat

我正在尝试执行需要反应性上下文(而不是整个服务器)的代码

Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Backtrace:
 1. shiny::withReactiveDomain(...) test_integration.R:45:2
 5. shiny:::v() test_integration.R:48:4
 6. rv$get()
 7. private$dependents$register()
 8. shiny:::getCurrentContext()
 9. .getReactiveEnvironment()$currentContext()

然后我得到以下错误:

Csv

我想念什么?如果我在R会话中执行内部块,则会遇到相同的错误。

2 个答案:

答案 0 :(得分:0)

具有未导出函数flushReact的解决方案:

library(shiny)

x <- reactiveVal()
observe({
  message(x())
})
x("abc")

capture.output(shiny:::flushReact(), type = "message")
# [1] "abc"

答案 1 :(得分:0)

@anddt解决方案

library(shiny)
library(testthat)

test_that("test ", {
  withReactiveDomain(MockShinySession$new(), {
    v <- reactiveVal()
    v("abc")
    val <- isolate(v())
    expect_equal("abc", val)
  })
})