[英]r devtools test() errors but testthat test_file() works
[英]R: Problems with testthat test_file() within local environment
我在本地環境中調用test_file時遇到問題。
local({ newvar <- 1; expect_equal(newvar, 1); });
工作正常。
local({
newvar <- 1;
test_that('newvar is equal to 1:', { expect_equal(newvar, 1) });
});
工作正常。
local( { newvar <- 1; test_file('simple.test.R'); });
錯誤:找不到對象'newvar'
simple.test.R的內容很簡單:
context('local env test');
test_that('local env test', { expect_equal(newvar, 1) })
幫助贊賞! 謝謝。
編輯:
我想要做的是從shinyAce( https://github.com/trestletech/shinyAce )讀取一些代碼,並檢查它是否有效(mets一些定義的要求)。 我正在使用'local()',因此在shinyAce塊中定義的任何指定變量都不會保留在環境中。
這是test_file
的源test_file
:
function (path, reporter = "summary")
{
reporter <- find_reporter(reporter)
with_reporter(reporter, {
sys.source(path, new.env(parent = globalenv()), chdir = TRUE)
end_context()
})
}
關鍵是這樣的:
sys.source(path, new.env(parent = globalenv()), chdir = TRUE)
該文件在全局環境下的新環境中執行,而newvar
僅在您創建的本地環境中可用。
你的最終目標究竟是什么? 我們可以嘗試提供幫助。 或者你只是好奇?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.