Testing shiny application

Hi all, I refering the link Shiny - Server function testing with Shiny where I am testing the code in server functions. But i am getting error as shown below

app <- shinyApp(
  ui = fluidPage(
    numericInput("x", "X", value = 5),
    textOutput("txt")
  ),
  server = function(input, output, session) {
    myreactive <- reactive({
      input$x * 2
    })
    output$txt <- renderText({
      paste0("I am ", myreactive())
    })
  }
)


testServer(server, {
  session$setInputs(x = 1)
  expect_equal(myreactive(), 2)
})

Error

 shiny::runApp()
Loading required package: shiny
Warning in loadSupport(appDir, renv = sharedEnv, globalrenv = NULL) :
  Loading R/ subdirectory for Shiny application, but this directory appears to contain an R package. Sourcing files in R/ may cause unexpected behavior.
Error in isModuleServer(app) : object 'server' not found

Can anyone help me here

There is an error in the documentation you are learning from :frowning:
change

testServer(server, {
  session$setInputs(x = 1)
  expect_equal(myreactive(), 2)
})

to

testServer(app, {
  session$setInputs(x = 1)
  expect_equal(myreactive(), 2)
})

Thanks. But i tried your input. I get below error

You need an app before you test it, like this one

Sorry I already had an app before.I forgot to mention

Its not clear that you ran the app script to create the app object in the environment so it would be present when you ran testServer. If you didnt then you should, if you did, then im at my limit and cant help

Thanks.

Can anyone help me here? I Ran app.R file and the application for generated. However i am getting this error

Hello,

I am also receiving the same error. However, I am testing a Shiny server module.

I made the Shiny app file structure as follows:

myapp/
|- app.R
|- R
|   |- module.R
`- tests
    `- testthat
        |- test-module.R

Where module.R contains the moduleUI and moduleServer code.

Then testServer tells me: Error in isModuleServer(app)`: object 'moduleServer' not found
Backtrace:

  1. shiny::testServer(...)
  2. shiny:::isModuleServer(app)`

I think something is wrong with my file structure but I cannot be certain. I followed the instructions how Hadley says in Chapter 21 of Mastering Shiny. We can also usethis::use_test() in our module to generate the file needed that has the tests.

-Cheers , Kraggle

I found that for the test to work, we must Source our module code.

There should be a Source button on the RStudio UI. After sourcing the module (in your case the app.R), the test ran successfully.

Good luck!

This topic was automatically closed 54 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.