Problems with testthat Connections

Does anyone have any idea why using textConnectioninside a test_thatfunction would not function correctly?

i.e. If I run the following code directly, everything works fine:

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

expect_equal(txt, "test data")  #Works

While if I insert this inside a function test_that, this does not work, and I get an empty txt variable when called expect_equal.

test_that("connection will work", {

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

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

Session Information

> 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 
+5
source share
1 answer

Try the following:

test_that("connection will work", {

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

I realized that the fact that test_thatevaluates the code in a separate environment was related to the problem, and then checked the documentation for textConnection.

+6
source

All Articles