I am trying to develop unit tests for my shiny app, which was made using golem
framework and shiny
v1.5.0.
Golem comes with a recommended test file, which covers very basic UI testing. I am, however, way more concerned with testing the server-side, for all modules. So just like you would make tests for each function in a regular R package, I want to test each module in my shiny app.
I know that there's a testModule
function in development, but it is not included within the latest shiny CRAN version (which is precisely the version I am using). There's only a testServer
function, but all the examples I found seem to define the module server side within the test file. I couldn't find an example of module testing when the module is part of a package.
So what I'd like to do is basically a test that lives inside tests/testthat/test-my_module.R
and would look like this:
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)
})
})
However, this throws an error:
Error in UseMethod("as.shiny.appobj", x) :
método não aplicável para 'as.shiny.appobj' aplicado a um objeto de classe "function"
which basically says the specified method does not work when applied to a function class object.
Am I missing something here?
Would appreciate some discussion on how you do tests when developing a shiny app as a package.
I was stuck with this too. I solved this way for my package (sistec).
You could try this in yours: