我正在尝试为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)