如何在 golem 框架闪亮包中测试模块?

edu*_*app 6 r testthat shiny golem

我正在尝试为我的闪亮应用程序开发单元测试,该应用程序是使用golem框架和shinyv1.5.0 制作的。

\n

Golem 附带了一个推荐的测试文件,其中涵盖了非常基本的 UI 测试。然而,我更关心测试所有模块的服务器端。因此,就像您对常规 R 包中的每个函数进行测试一样,我想在我闪亮的应用程序中测试每个模块。

\n

我知道有一个testModule功能正在开发中,但它没有包含在最新的闪亮 CRAN 版本中(这正是我正在使用的版本)。只有一个testServer函数,但我发现的所有示例似乎都在测试文件中定义了模块服务器端。当模块是包的一部分时,我找不到模块测试的示例。

\n

所以我想做的基本上是一个位于内部的测试tests/testthat/test-my_module.R,看起来像这样:

\n
\ntest_that("The module receives its input", {\n    shiny::testServer(\n        app = mypackage::my_module_server,\n        args = list(),\n        session = MockShinySession$new(), {\n        session$setInputs(some_input = 100)\n        expect_equal(output$some_output, 50)\n    })\n})\n
Run Code Online (Sandbox Code Playgroud)\n

但是,这会引发错误:

\n
Error in UseMethod("as.shiny.appobj", x) : \n  m\xc3\xa9todo n\xc3\xa3o aplic\xc3\xa1vel para 'as.shiny.appobj' aplicado a um objeto de classe "function"\n
Run Code Online (Sandbox Code Playgroud)\n

这基本上表明指定的方法在应用于函数类对象时不起作用。

\n

我在这里错过了什么吗?

\n

希望能讨论一下在将闪亮的应用程序开发为包时如何进行测试。

\n

小智 8

我也被这个问题困扰了。我用这种方法解决了我的包(sistec)。

aria_server <- sistec::aria_server() 
server <- function(id) {
  shiny::moduleServer(id, aria_server)
}

testServer(server, {
  # comparison$x is FALSE
  testthat::expect_false(comparison$x)
})
Run Code Online (Sandbox Code Playgroud)

你可以在你的中尝试这个:

mypackage_server <- mypackage::my_module_server
server <- function(id) {
  moduleServer(id, mypackage)
}

test_that("The module receives its input", {
    shiny::testServer(server, {
      session$setInputs(some_input = 100)
      expect_equal(output$some_output, 50)
    })
})
Run Code Online (Sandbox Code Playgroud)