2013-04-02 15 views
5

Chiunque ha qualche idea sul perché l'uso di textConnection all'interno di una funzione test_that non funzioni correttamente?Problemi con testthat Collegamenti

cioè Se eseguo direttamente il seguente codice, tutto funziona alla grande:

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

expect_equal(txt, "test data") #Works 

Mentre se mi nido questo all'interno di una funzione test_that, non funziona e ricevo una variabile txt vuoto giunto il expect_equal chiamata.

test_that("connection will work", { 

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

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

Info Sessione

> 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 

risposta

6

Prova questo:

test_that("connection will work", { 

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

sono arrivato dal mio sospetto che il fatto che test_that valuta il codice all'interno di un ambiente separato era collegato al problema e quindi controllare la documentazione su textConnection.

+0

+ 1 elimina la mia risposta (quasi completata) (anche se la mia logica ha verificato prima la documentazione di 'textConnection') – mnel

+0

Questo ha fatto il trucco! Grazie. –