4

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

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

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

所以我想做的基本上是一个生活在里面的测试,tests/testthat/test-my_module.R看起来像这样:


test_that("The module receives its input", {
    shiny::testServer(
        app = mypackage::my_module_server,
        args = list(),
        session = MockShinySession$new(), {
        session$setInputs(some_input = 100)
        expect_equal(output$some_output, 50)
    })
})

但是,这会引发错误:

Error in UseMethod("as.shiny.appobj", x) : 
  método não aplicável para 'as.shiny.appobj' aplicado a um objeto de classe "function"

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

我在这里错过了什么吗?

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

4

1 回答 1

4

I was stuck with this too. I solved this way for my package (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)
})

You could try this in yours:

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)
    })
})
于 2020-12-12T12:45:34.100 回答