Suppose I need a custom wait testthat. For example, I am testing many objects to see if they have missing values. testhatthe way things are written should be something like this:
expect_no_nas <- function(object, info = NULL, label = NULL)
{
lab <- testthat:::make_label(object, label)
expect(has_no_nas(object), sprintf("%s has nulls.", lab),
info = info)
invisible(object)
}
has_no_nas <- function()
{
!any(is.na(x))
}
How can I verify that this is correct?
I can write tests that pass without problems.
test_that(
"expect_no_nas passes when there are no NAs",
{
expect_no_nas(1:5)
}
)
It seemed to me that I can wrap user expectation in expect_error, but this will not work:
test_that(
"expect_no_nas fails when there are NAs",
{
expect_error(expect_no_nas(c(1, NA)))
}
)
#
#
#
The wrapper in trydoes not work either.
test_that(
"expect_no_nas fails when there are NAs",
{
res <- try(expect_no_nas(c(1, NA)))
expect_false(res$passed)
}
)
How to check for errors? (It’s important to remember that we are testing to see if expect_no_nas, and not just writing, the tests that use expect_no_nas.)
source
share