运行 `testServer()`,并使用 `golem::get_golem_options()`。

huangapple go评论54阅读模式
英文:

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_variableNULL(这也有点合理):

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 &lt;- function(input, output, session) {

  myServer(
    id = &quot;test&quot;,
    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操作之前,您需要执行以下三个步骤:

  1. 设置您的golem_options
set_testing_golem_options(list(global_variable = 'somevalue'))
  • 或者,您可以创建一个不包含上述函数的选项列表,但该函数将尊重已经设置的任何其他shinyOptions
  1. 明确定义您要用于测试的MockShinySession
defined_mock_shiny_session <- MockShinySession$new()
  1. 将选项分配到模拟会话中:
defined_mock_shiny_session$options[['golem_options']] <- getShinyOption("golem_options")
  1. 现在的关键是使用刚刚创建的显式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:

#&#39; Set golem options for unit testing purposes
#&#39;
#&#39; @param golem_opts list()
#&#39;
#&#39; @examples
#&#39; \dontrun{
#&#39;   my_golem_options &lt;-
#&#39;   list(
#&#39;     myconfig = &quot;config01&quot;
#&#39;   )
#&#39;   set_testing_golem_options(my_golem_options)
#&#39; }
set_testing_golem_options &lt;-
  function (golem_opts)
  {
    current_golem_options = getShinyOption(&quot;golem_options&quot;)
    for (n in names(golem_opts)) {
      current_golem_options[[n]] &lt;- 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:

  1. Set your golem_options:
set_testing_golem_options(list(global_variable = &#39;somevalue&#39;))
  • 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.
  1. Explicitly define the MockShinySession that you're going to use for testing:
defined_mock_shiny_session &lt;- MockShinySession$new()
  1. Assign the options into the mock session:
defined_mock_shiny_session$options[[&#39;golem_options&#39;]] &lt;-
  getShinyOption(&quot;golem_options&quot;)
  1. Now the trick is to call the testServer with the explicit MockShinySession 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 &lt;-
  function (golem_opts)
  {
    current_golem_options = getShinyOption(&quot;golem_options&quot;)
    for (n in names(golem_opts)) {
      current_golem_options[[n]] &lt;- golem_opts[[n]]
    }
    shinyOptions(golem_options = current_golem_options)
  }

# Set the golem_options that you need
set_testing_golem_options(list(global_variable = &#39;somevalue&#39;))

# Create the mock session
defined_mock_shiny_session &lt;- MockShinySession$new()

# Assign the golem_options
defined_mock_shiny_session$options[[&#39;golem_options&#39;]] &lt;-
  getShinyOption(&quot;golem_options&quot;)

# 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
)

huangapple
  • 本文由 发表于 2023年2月6日 21:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定