Can I acess a downloadHandler filename() or content() when writing tests?

I'm writing tests using shiny::testServer() to test that my modules work as intended. The module is used to create certain reports in online tables, as well as to provide a way to download those tables as .xlsx files.

Is there a way to check if the downloaded file is correct and occurs without error? I have a downloadHandler as output$download. The content is a function that applies some logic before writing to a .xslx file. How can I simulate the user clicking on the download button and check the resulting file?

I'm not sure whether you can do that with shiny::testServer() but you definitely can with {shinytest2}. See the example below which returns a path with the same filename as used in the app. You can then open that file and check whatever you need to.

library(shinytest2)
app_path <- system.file("examples/10_download", package = "shiny")
app <- AppDriver$new(app_path)

# Get rock.csv as a tempfile
app$get_download("downloadData")
#> [1] "/TEMP/PATH/rock.csv"

I've found out that I can access output$id values inside of tests. The specific value type depends on the output, but for a downloadHandler it's the name of a temp file created by running the download functionality. That's exactly what I needed! So I can test it with

{r}
expect_no_error({temp_xlsx <- output$download})

to make sure no error is raised, and I could load the file with loadWorkbook(temp_xlsx) if I needed to test the content.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.