代码之家  ›  专栏  ›  技术社区  ›  bertolami

测试中的反应上下文

  •  0
  • bertolami  · 技术社区  · 4 年前

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

    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()
    

    我错过了什么?如果我在R会话中执行内部块,我会得到相同的错误。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Stéphane Laurent    4 年前

    具有未导出函数的解 flushReact :

    library(shiny)
    
    x <- reactiveVal()
    observe({
      message(x())
    })
    x("abc")
    
    capture.output(shiny:::flushReact(), type = "message")
    # [1] "abc"
    
        2
  •  0
  •   bertolami    4 年前

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