3

ローカル環境内での 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 ) からいくつかのコードを読み取り、それが有効であることを確認することです (いくつかの定義された要件を満たしています)。'local()' を使用して、shinyAce ブロックで定義された割り当てられた変数が環境に残らないようにしました。

4

1 に答える 1

2

のソースは次の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が、作成したローカル環境でのみ使用できます。

ここでの最終目標は正確には何ですか?私たちは助けることができます。それともただ興味があっただけですか?

于 2013-11-15T03:26:23.993 に答える