| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/test-server.R |
| 3 |
\name{testServer} |
| 4 |
\alias{testServer} |
| 5 |
\title{Reactive testing for Shiny server functions and modules} |
| 6 |
\usage{ |
| 7 |
testServer(app = NULL, expr, args = list(), session = MockShinySession$new()) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{app}{A server function (i.e. a function with \code{input}, \code{output}, |
| 11 |
and \code{session}), or a module function (i.e. a function with first |
| 12 |
argument \code{id} that calls \code{\link[=moduleServer]{moduleServer()}}. |
| 13 |
|
| 14 |
You can also provide an app, a path an app, or anything that |
| 15 |
\code{\link[=as.shiny.appobj]{as.shiny.appobj()}} can handle.} |
| 16 |
|
| 17 |
\item{expr}{Test code containing expectations. The objects from inside the |
| 18 |
server function environment will be made available in the environment of |
| 19 |
the test expression (this is done using a data mask with |
| 20 |
\code{\link[rlang:eval_tidy]{rlang::eval_tidy()}}). This includes the parameters of the server function |
| 21 |
(e.g. \code{input}, \code{output}, and \code{session}), along with any other values |
| 22 |
created inside of the server function.} |
| 23 |
|
| 24 |
\item{args}{Additional arguments to pass to the module function. If \code{app} is |
| 25 |
a module, and no \code{id} argument is provided, one will be generated and |
| 26 |
supplied automatically.} |
| 27 |
|
| 28 |
\item{session}{The \code{\link{MockShinySession}} object to use as the \link[=domains]{reactive domain}. The same session object is used as the domain both |
| 29 |
during invocation of the server or module under test and during evaluation |
| 30 |
of \code{expr}.} |
| 31 |
} |
| 32 |
\description{ |
| 33 |
A way to test the reactive interactions in Shiny applications. Reactive |
| 34 |
interactions are defined in the server function of applications and in |
| 35 |
modules. |
| 36 |
} |
| 37 |
\examples{ |
| 38 |
# Testing a server function ---------------------------------------------- |
| 39 |
server <- function(input, output, session) { |
| 40 |
x <- reactive(input$a * input$b) |
| 41 |
} |
| 42 |
|
| 43 |
testServer(server, { |
| 44 |
session$setInputs(a = 2, b = 3) |
| 45 |
stopifnot(x() == 6) |
| 46 |
}) |
| 47 |
|
| 48 |
|
| 49 |
# Testing a module -------------------------------------------------------- |
| 50 |
myModuleServer <- function(id, multiplier = 2, prefix = "I am ") { |
| 51 |
moduleServer(id, function(input, output, session) { |
| 52 |
myreactive <- reactive({ |
| 53 |
input$x * multiplier |
| 54 |
}) |
| 55 |
output$txt <- renderText({ |
| 56 |
paste0(prefix, myreactive()) |
| 57 |
}) |
| 58 |
}) |
| 59 |
} |
| 60 |
|
| 61 |
testServer(myModuleServer, args = list(multiplier = 2), { |
| 62 |
session$setInputs(x = 1) |
| 63 |
# You're also free to use third-party |
| 64 |
# testing packages like testthat: |
| 65 |
# expect_equal(myreactive(), 2) |
| 66 |
stopifnot(myreactive() == 2) |
| 67 |
stopifnot(output$txt == "I am 2") |
| 68 |
|
| 69 |
session$setInputs(x = 2) |
| 70 |
stopifnot(myreactive() == 4) |
| 71 |
stopifnot(output$txt == "I am 4") |
| 72 |
# Any additional arguments, below, are passed along to the module. |
| 73 |
}) |
| 74 |
} |