I've a character vector that uses a custom class foo
.
bar <- structure("foo", class = c("foo", "character"))
When comparing this object against the string "foo"
, the test complains that classes don't match.
test_that("foo", {
expect_equal(bar, "foo")
})
#> ── Failure (Line 2): foo ───────────────────────────────────────────────────────
#> structure("foo", class = c("foo", "character")) (`actual`) not equal to "foo" (`expected`).
#>
#> `actual` is an S3 object of class <foo/character>, a character vector
#> `expected` is a character vector ('foo')
I've ended up using the ignore_attr = TRUE
parameter in expect_equal()
but it's not convenient because I have to use it in many tests (the waldo::compare()
doc also recommend not to use it for good reasons).
I've noticed that testing glue
strings (classes glue/character
) work so there might be a way to make my test work without ignoring attributes.
test_that("foo", {
expect_equal(glue("foo"), "foo")
})
#> Test passed
How can I make my test pass without ignoring attributes or unclasing the input object?