test_that调用内部的评估

时间:2016-03-28 02:47:58

标签: r testthat

我正在尝试为my package编写一些单元测试,并且难以从gls开始测试nlme函数。 MWE:

library(testthat)
library(nlme)
data(Ovary, package = "nlme")
test_that("getData works.", {
  re_order <- sample(nrow(Ovary))
  egg_scramble <- Ovary[re_order,]
  gls_scramble <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), 
                     data = egg_scramble)
  dat <- getData(gls_scramble)
  expect_identical(egg_scramble, dat)
})

由于某种原因,getData调用无法在测试环境中找到数据。这是追溯:

Error: Test failed: 'getData works.'
Not expected: object 'egg_scramble' not found
1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls)
2: eval(code, new_test_environment)
3: eval(expr, envir, enclos)
4: getData(gls_scramble) at :6
5: getData.gls(gls_scramble)
6: eval(if ("data" %in% names(object)) object$data else mCall$data)
7: eval(expr, envir, enclos).

然而,评估test_that之外的相同代码不会导致错误:

re_order <- sample(nrow(Ovary))
egg_scramble <- Ovary[re_order,]
gls_scramble <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), 
                    data = egg_scramble)
dat <- getData(gls_scramble)
identical(dat, egg_scramble)

0 个答案:

没有答案