2013-04-02 53 views
5

任何人有任何想法,爲什麼在test_that函數內使用textConnection將無法​​正常工作?Testthat連接問題

即如果我直接運行下面的代碼,一切都很正常:

txt <- "" 
con <- textConnection("txt", "w") 
writeLines("test data", con) 
close(con) 

expect_equal(txt, "test data") #Works 

而如果我窩這個test_that函數內,它不工作,我得到一個空的TXT變量來的expect_equal呼叫。

test_that("connection will work", { 

    txt <- "" 
    con <- textConnection("txt", "w") 
    writeLines("test data", con) 
    close(con) 

    expect_equal(txt, "test data") 
}) #Fails 

會議信息

> sessionInfo() 
R version 2.15.2 (2012-10-26) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
    [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 
[4] LC_NUMERIC=C       LC_TIME=English_United States.1252  

attached base packages: 
    [1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
    [1] testthat_0.7 RODProt_0.1.1 rjson_0.2.12 

loaded via a namespace (and not attached): 
    [1] evaluate_0.4.2 plyr_1.8  stringr_0.6.1 tools_2.15.2 

回答

6

試試這個:

test_that("connection will work", { 

    txt <- "" 
    con <- textConnection("txt", "w",local = TRUE) 
    writeLines("test data", con) 
    close(con) 
    expect_equal(txt, "test data") 
}) 

我從我預感到了那裏的事實,test_that計算一個單獨的環境中的代碼被連接的問題,然後檢查文檔textConnection

+0

+ 1刪除我的(幾乎完成)響應(儘管我的邏輯首先檢查了「textConnection」的文檔) – mnel

+0

這樣做了!謝謝。 –