2

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 Answers1

3

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!

nicola
  • 24,005
  • 3
  • 35
  • 56
  • Okay, but what should I do to check likelihood, riskMessage, and color variables? If I don't check these, what should I check instead? – user14057357 Aug 25 '20 at 15:52
  • Make them "global" variables (i.e. defined directly in `server` and not in a function inside `server`). – nicola Aug 26 '20 at 04:50
  • I'm not sure how I would make them global variables because I keep getting an error. This is the error that I get: `Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)`. If you're able to edit your response so I can see what it should look like, I would appreciate it. – user14057357 Aug 26 '20 at 06:17
  • @user14057357 Made an edit with a correct use of the variables. – nicola Aug 26 '20 at 07:50
  • I've been learning about integration testing, and I'm still a little confused about a couple of things. How does this type of testing work exactly? Is there a virtual web browser? – user14057357 Aug 27 '20 at 19:37
  • No, this is totally server-side. It just calculates the variables given that some inputs have changed. No data is exchanged to the outside. – nicola Aug 28 '20 at 07:53
  • so how does this compare to Selenium? – user14057357 Aug 28 '20 at 18:24