Souring other files in test server function

Hi all,

I have a package built so that I am able to test functions using test that. I have 2 scenarios where I test function. One scenario works fine (Sce. A) and other (Sce. B) does not work

Sce.B

## app.R (location : D:/Windows/Analytics/R Programming/GitHub/App/pacakge1)
library(shiny)

ui <- fluidPage(
  numericInput("x", "X", value = 5),
  textOutput("txt"),
  actionButton("button", "Submit")
)

server <- function(input, output, session) {

  server_1(input, output, session , y1)

}

shinyApp(ui, server)
## file. R (Location : D:/Windows/Analytics/R Programming/GitHub/App/pacakge1/R
server_1 <- function(input, output, session , y1){
  y1 <- reactiveValues(a = 0)
  function1 <- function(){
    y1$a = 2 * input$x
  }

  observeEvent(input$button,{
    function1()
  })

  output$txt <- renderText({
    y1$a
  })

}
## test server (location : D:/Windows/Analytics/R Programming/GitHub/App/pacakge1/tests/testthat)

library(testthat)
library(shinytest)
library(shiny)

testServer(expr = {
  # y1 <- session$getReturned()
  session$setInputs(x = 7)
  function1()
  expect_equal(y1$a, 14)
})

When I test above above scenario (Sce. B). It does not work well. So I need to test if y1$a returns 14 or not
But scenario A below works well since I am not writing server function outside

Sce.A

##app.R (Same location as above)
library(shiny)

ui <- fluidPage(
  numericInput("x", "X", value = 5),
  textOutput("txt"),
  actionButton("button", "Submit")
)

server <- function(input, output, session) {

  y1 <- reactiveValues(a = 0)
  function1 <- function(){
    y1$a = 2 * input$x
  }

  observeEvent(input$button,{
    function1()
  })

  output$txt <- renderText({
    y1$a
  })

}

shinyApp(ui, server)

##test server (same location as above)
library(testthat)
library(shinytest)
library(shiny)

testServer(expr = {
  # y1 <- session$getReturned()
  session$setInputs(x = 7)
  function1()
  expect_equal(y1$a, 14)
})

Above test passes and allworks well..

But Scenario B is not working.

So the question is here, can we not test functions from other files?

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