Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with testthat Connections

Tags:

r

testthat

Anyone have any ideas why using textConnection inside of a test_that function would not function properly?

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

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

expect_equal(txt, "test data")  #Works

Whereas if I nest this inside of a test_that function, it doesn't work and I get an empty txt variable come the expect_equal call.

test_that("connection will work", {

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

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

Session Info

> 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 
like image 303
Jeff Allen Avatar asked Apr 02 '13 22:04

Jeff Allen


1 Answers

Try this:

test_that("connection will work", {

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

I got there from my hunch that the fact that test_that evaluates code within a separate environment was connected to the problem, and then checking the documentation on textConnection.

like image 95
joran Avatar answered Oct 20 '22 20:10

joran