【问题标题】:Problems with testthat Connections测试连接的问题
【发布时间】:2013-03-24 10:22:55
【问题描述】:

有人知道为什么在test_that 函数中使用textConnection 不能正常工作吗?

即如果我直接运行以下代码,一切正常:

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

expect_equal(txt, "test data")  #Works

而如果我将它嵌套在 test_that 函数中,它就不起作用,并且在 expect_equal 调用中我得到一个空的 txt 变量。

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 

【问题讨论】:

    标签: r testthat


    【解决方案1】:

    试试这个:

    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 上的文档。

    【讨论】:

    • + 1 删除我(几乎完成)的回复(尽管我的逻辑首先检查了textConnection 的文档)
    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多