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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With