Integration testing on Shiny apps

Viewed 227

I'm trying to use integration testing on this shiny app, but after reading up on it, I'm not sure what I'm doing wrong. I keep getting this error: Error in isModuleServer(app) : object 'session' not found I'm also getting this error: Error: object 'output' not found

Any help is appreciated.

library(shiny)
displayColoredBox<- function(color, riskMessage){
  sidebarPanel(style=sprintf("background-color: %s; width: 300px; height: 300px;", color),
               h3(sprintf("%s", riskMessage)) )  }
shinyApp(
  ui = fluidPage(
    
    div(
      id = "form",
      sliderInput("count1", "First Slider Input", value=0, min=0, max=5000),
      sliderInput("count2", "Second Slider Input", value=0, min=0, max=5000),
      uiOutput("coloredBox")
    )),
  
  server <- function(input, output) {
    
    output$coloredBox<-renderUI({
      req(input$count1)
      req(input$count2)
      
      count1 <- input$count1;
      count2 <- input$count2;
      
      likelihood <- (count1*count2)/5000000
      
      
      if (likelihood>1) {
        color="red"
        riskMessage="Extreme risk!"
        
      } else if (likelihood>.65){
        color="orange"
        riskMessage="Very high risk!"
      }
      else if (likelihood>.35){
        color="yellow"
        riskMessage="High risk!"
      }
      else if (likelihood>.10){
        color="blue"
        riskMessage="Moderate risk!"
      } else {
        color="green"
        riskMessage="Low risk!"
      }
      
      displayColoredBox(color, riskMessage)
      
    })
  }
)

testServer({
  session$setInputs(count1 = 1500)
  session$setInputs(count2 = 2500)
  
  stopifnot(output$likelihood == 0.75)
  stopifnot(output$riskMessage == "Very high risk!")
  stopifnot(output$color == "orange")
  
})
1 Answers

Assign your app to an object, which must be the first argument of testServer. Also, declare your server function to have a session argument.

Edit: in order to use your variables in a shinyTest, we make use of a reactive which returns a list with the components you want to test.

app <- shinyApp(
  ui = fluidPage(
    
    div(
      id = "form",
      sliderInput("count1", "First Slider Input", value=0, min=0, max=5000),
      sliderInput("count2", "Second Slider Input", value=0, min=0, max=5000),
      uiOutput("coloredBox")
    )),
  
  server <- function(input, output, session) {
    getRiskAndColor<-reactive({
      req(input$count1)
      req(input$count2)
      
      count1 <- input$count1;
      count2 <- input$count2;
      
      likelihood <- (count1*count2)/5000000
      
      
      if (likelihood>1) {
        color="red"
        riskMessage="Extreme risk!"
        
      } else if (likelihood>.65){
        color="orange"
        riskMessage="Very high risk!"
      }
      else if (likelihood>.35){
        color="yellow"
        riskMessage="High risk!"
      }
      else if (likelihood>.10){
        color="blue"
        riskMessage="Moderate risk!"
      } else {
        color="green"
        riskMessage="Low risk!"
      }
      list(color=color, riskMessage=riskMessage, likelihood=likelihood)
    })
    
    output$coloredBox<-renderUI({
      colorRisk<-getRiskAndColor()
      displayColoredBox(colorRisk$color, colorRisk$riskMessage)
      
    })
  }
)

Next, we feed testServer with app. Notice that the getRiskAndColor reactive value is accessible, with its components.

testServer(app, {
  session$setInputs(count1 = 1500)
  session$setInputs(count2 = 2500)
  rc<-getRiskAndColor()
  stopifnot(rc$likelihood == 0.75)
  cat("Correct likelihood value!\n")
  stopifnot(rc$riskMessage == "Very high risk!")
  cat("Correct risk message!\n")
  stopifnot(rc$color == "orange")
  cat("Correct color!\n")
})

#Correct likelihood value!
#Correct risk message!
#Correct color!    

Success!

Related