英文:
Run `testServer()` w/ `golem::get_golem_options()`
问题
我已经使用golem R包设置了一个R Shiny应用程序。在app_server()
中,我使用了一些全局设置的golem选项,如下所示:
app_server <- function(input, output, session) {
myServer(
id = "test",
global_variable = golem::get_golem_options()$global_variable,
other_variable = other_variable
)
...
}
现在,在test-golem-recommended.R
中,默认的测试testServer()
抛出错误,因为在myServer()
中找不到golem选项,或者global_variable
为NULL
(这也有点合理):
testServer(app_server, {
# 设置并测试一个输入
session$setInputs(x = 2)
expect_equal(input$x, 2)
})
然而,是否可能调用app_server()
以便首先加载golem选项?
英文:
I have set up a R Shiny app using the golem R-package. In the app_server()
I use some globally set golem options as follows:
app_server <- function(input, output, session) {
myServer(
id = "test",
global_variable = golem::get_golem_options()$global_variable,
other_variable = other_variable
)
...
}
Now the default test testServer()
in test-golem-recommended.R
throws an error because within myServer()
it can't find the golem options or the global_variable
is NULL
(which also kind of makes sense):
testServer(app_server, {
# Set and test an input
session$setInputs(x = 2)
expect_equal(input$x, 2)
})
However, is it possible to call app_server()
so that the golem options are loaded first?
答案1
得分: 1
我遇到了相同的问题,有一个不太优雅的解决方案,涉及在单元测试阶段明确设置golem_options
。
首先,我定义了一个函数来设置golem_options
。这可以放在您的包的任何位置,或在testthat
脚本中定义:
# 设置用于单元测试目的的golem选项
set_testing_golem_options <- function(golem_opts) {
current_golem_options <- getShinyOption("golem_options")
for (n in names(golem_opts)) {
current_golem_options[[n]] <- golem_opts[[n]]
}
shinyOptions(golem_options = current_golem_options)
}
然后,在test-golem-recommended.R
文件中,在testServer
操作之前,您需要执行以下三个步骤:
- 设置您的
golem_options
:
set_testing_golem_options(list(global_variable = 'somevalue'))
- 或者,您可以创建一个不包含上述函数的选项列表,但该函数将尊重已经设置的任何其他
shinyOptions
。
- 明确定义您要用于测试的
MockShinySession
:
defined_mock_shiny_session <- MockShinySession$new()
- 将选项分配到模拟会话中:
defined_mock_shiny_session$options[['golem_options']] <- getShinyOption("golem_options")
- 现在的关键是使用刚刚创建的显式
MockShinySession
来调用testServer
:
testServer(
app_server,
{
# 设置和测试一个输入
session$setInputs(x = 2)
expect_equal(input$x, 2)
},
session = defined_mock_shiny_session
)
这个模式对我有用。在测试完成后,您可能需要明确取消任何golem_options
,或以某种方式确保它们恢复到其之前的状态。但希望这能帮助您解决问题。
以下是使用您提供的示例的所有内容都在一个代码块中:
# 定义设置golem_options的函数
set_testing_golem_options <- function(golem_opts) {
current_golem_options <- getShinyOption("golem_options")
for (n in names(golem_opts)) {
current_golem_options[[n]] <- golem_opts[[n]]
}
shinyOptions(golem_options = current_golem_options)
}
# 设置您需要的golem_options
set_testing_golem_options(list(global_variable = 'somevalue'))
# 创建模拟会话
defined_mock_shiny_session <- MockShinySession$new()
# 分配golem_options
defined_mock_shiny_session$options[['golem_options']] <- getShinyOption("golem_options")
# 执行测试
testServer(
app_server,
{
# 设置和测试一个输入
session$setInputs(x = 2)
expect_equal(input$x, 2)
},
session = defined_mock_shiny_session
)
英文:
I encountered the same issue, and have an inelegant solution that involves setting the golem_options
explicitly during the unit testing phase.
First, I defined a function to set the golem_options
. This can be anywhere in your package, or defined in the testthat
script:
#' Set golem options for unit testing purposes
#'
#' @param golem_opts list()
#'
#' @examples
#' \dontrun{
#' my_golem_options <-
#' list(
#' myconfig = "config01"
#' )
#' set_testing_golem_options(my_golem_options)
#' }
set_testing_golem_options <-
function (golem_opts)
{
current_golem_options = getShinyOption("golem_options")
for (n in names(golem_opts)) {
current_golem_options[[n]] <- golem_opts[[n]]
}
shinyOptions(golem_options = current_golem_options)
}
Then you want to do 3 things just before the testServer
action in the test-golem-recommended.R
:
- Set your
golem_options
:
set_testing_golem_options(list(global_variable = 'somevalue'))
- Alternatively, you could just make a list of options without the function listed above, but the function will respect any other
shinyOptions
that happen to be already set.
- Explicitly define the
MockShinySession
that you're going to use for testing:
defined_mock_shiny_session <- MockShinySession$new()
- Assign the options into the mock session:
defined_mock_shiny_session$options[['golem_options']] <-
getShinyOption("golem_options")
- Now the trick is to call the
testServer
with the explicitMockShinySession
that you've just created:
testServer(
app_server,
{
# Set and test an input
session$setInputs(x = 2)
expect_equal(input$x, 2)
},
session = defined_mock_shiny_session
)
That pattern works for me. I should probably explicitly unset any golem_options
after the test is complete, or ensure that they revert back to their previous state in some way. But hopefully this will get you unstuck.
Here's everything all in one code block using the example you provided:
# Define the function to set golem_options
set_testing_golem_options <-
function (golem_opts)
{
current_golem_options = getShinyOption("golem_options")
for (n in names(golem_opts)) {
current_golem_options[[n]] <- golem_opts[[n]]
}
shinyOptions(golem_options = current_golem_options)
}
# Set the golem_options that you need
set_testing_golem_options(list(global_variable = 'somevalue'))
# Create the mock session
defined_mock_shiny_session <- MockShinySession$new()
# Assign the golem_options
defined_mock_shiny_session$options[['golem_options']] <-
getShinyOption("golem_options")
# Execute the test
testServer(
app_server,
{
# Set and test an input
session$setInputs(x = 2)
expect_equal(input$x, 2)
},
session = defined_mock_shiny_session
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论