Mock Shiny Session — MockShinySession
R/mock-session.R
Description
An R6 class suitable for testing purposes. Simulates, to the
extent possible, the behavior of the ShinySession class. The session
parameter provided to Shiny server functions and modules is an instance of
a ShinySession in normal operation.
Most kinds of module and server testing do not require this class be
instantiated manually. See instead testServer().
In order to support advanced usage, instances of MockShinySession are
unlocked so that public methods and fields of instances may be
modified. For example, in order to test authentication workflows, the
user or groups fields may be overridden. Modified instances of
MockShinySession may then be passed explicitly as the session argument
of testServer().
Public fields
envThe environment associated with the session.
returnedThe value returned by the module under test.
singletonsHardcoded as empty. Needed for rendering HTML (i.e. renderUI).
clientDataMock client data that always returns a size for plots.
outputThe shinyoutputs associated with the session.
inputThe reactive inputs associated with the session.
userDataAn environment initialized as empty.
progressStackA stack of progress objects.
tokenOn a real
ShinySession, used to identify this instance in URLs.cacheThe session cache object.
appcacheThe app cache object.
restoreContextPart of bookmarking support in a real
ShinySessionbut alwaysNULLfor aMockShinySession.groupsCharacter vector of groups associated with an authenticated user. Always
NULLfor aMockShinySesion.userThe username of an authenticated user. Always
NULLfor aMockShinySession.optionsA list containing session-level shinyOptions.
Active bindings
filesFor internal use only.
downloadsFor internal use only.
closedDeprecated in
ShinySessionand signals an error.sessionDeprecated in ShinySession and signals an error.
requestAn empty environment where the request should be. The request isn't meaningfully mocked currently.
Methods
Method onFlush()
Define a callback to be invoked before a reactive flush
Method onFlushed()
Define a callback to be invoked after a reactive flush
Method setInputs()
Sets reactive values associated with the session$inputs
object and flushes the reactives.
Arguments
...The inputs to set. These arguments are processed with
rlang::list2()and so are dynamic. Input names may not be duplicated.
Method .scheduleTask()
An internal method which shouldn't be used by others.
Schedules callback for execution after some number of millis
milliseconds.
Method defineOutput()
An internal method which shouldn't be used by others. Defines an output in a way that sets private$currentOutputName appropriately.
Method getOutput()
An internal method which shouldn't be used by others. Forces evaluation of any reactive dependencies of the output function.
Method setEnv()
Set the environment associated with a testServer() call, but only if it has not previously been set. This ensures that only the environment of the outermost module under test is the one retained. In other words, the first assignment wins.
Method setReturned()
Set the value returned by the module call and proactively flush. Note that this method may be called multiple times if modules are nested. The last assignment, corresponding to an invocation of setReturned() in the outermost module, wins.
Method onSessionEnded()
Registers the given callback to be invoked when the session is closed (i.e. the connection to the client has been severed). The return value is a function which unregisters the callback. If multiple callbacks are registered, the order in which they are invoked is not guaranteed.
Method registerDownload()
Associated a downloadable file with the session.
Arguments
nameThe un-namespaced output name to associate with the downloadable file.
filenameA string or function designating the name of the file.
contentTypeA string of the content type of the file. Not used by
MockShinySession.contentA function that takes a single argument file that is a file path (string) of a nonexistent temp file, and writes the content to that file path. (Reactive values and functions may be used from this function.)
Method getCurrentOutputInfo()
Get information about the output that is currently being executed.