我正在设置测试。 我对测试数据进行操作,然后想确保正确的值出现在tibble的单元格中。 我认为有一个更简洁的方法。使用这个例子 band_instruments
library(tidyverse)
test_that("Musicians play instruments", {
expect_equal(band_instruments %>% filter(name == "Paul") %>% pull("plays"),
"bass")
expect_equal({band_instruments %>% filter(name == "Keith")}$plays,
"guitar")
})
这个可行,但是太长,太啰嗦。 怎样做这样的测试才是最简洁而又易读的呢?
解决方案:
这在我看来相当整洁。
test_that("Musicians play instruments", {
expect_equal(with(band_instruments, plays[name == "Paul"]), "bass")
expect_equal(with(band_instruments, plays[name == "Keith"]), "guitar")})
或者这样
with(band_instruments, test_that("Musicians play instruments", {
expect_equal(plays[name == "Paul"], "bass")
expect_equal(plays[name == "Keith"], "guitar")}))
本文来自投稿,不代表实战宝典立场,如若转载,请注明出处:https://www.shizhanbaodian.com/40391.html