我的单元测试无法比较参考和预期的SummarizedExperiment
对象。错误讯息:
> expect_identical(target, current)
Error: `target` not identical to `current`.
Attributes: < Component “assays”: Class definitions are not identical >
MWE:
advice you found on the internet
代码:
load("se-comparison.Rdata")
library(SummarizedExperiment)
library(testthat)
expect_identical(target, current)
# expect_identical() uses attr.all.equal() to compare S4 objects so check this
attr.all.equal(target, current)
# ok, check the attributes
cur <- attributes(current)
tar <- attributes(target)
class(cur$assays)
class(tar$assays)
expect_identical(
cur$assays,
tar$assays
)
expect_identical(
class(cur$assays),
class(tar$assays)
)
输出:
> library(SummarizedExperiment)
> library(testthat)
> expect_identical(target, current)
Error: `target` not identical to `current`.
Attributes: < Component “assays”: Class definitions are not identical >
> # expect_identical() uses attr.all.equal() to compare S4 objects so check this
> attr.all.equal(target, current)
[1] "Attributes: < Component “assays”: Class definitions are not identical >"
> # ok, check the attributes
> cur <- attributes(current)
> tar <- attributes(target)
> class(cur$assays)
[1] "ShallowSimpleListAssays"
attr(,"package")
[1] "SummarizedExperiment"
> class(tar$assays)
[1] "ShallowSimpleListAssays"
attr(,"package")
[1] "SummarizedExperiment"
> cur$assays
Reference class object of class "ShallowSimpleListAssays"
Field "data":
List of length 1
names(1): counts
> tar$assays
Reference class object of class "ShallowSimpleListAssays"
Field "data":
List of length 1
names(1): counts
> expect_identical(
+ cur$assays,
+ tar$assays
+ )
Error: cur$assays not identical to tar$assays.
Class definitions are not identical
> expect_identical(
+ class(cur$assays),
+ class(tar$assays)
+ )
>
为什么比较失败了?
答案 0 :(得分:2)
不确定为什么它一般会失败,但attr.all.equal
会比较getClass
方法返回的类定义(如果可用)。在这种情况下,每个getClass
对象的$assays
返回fieldPrototypes
和refMethods
不同的类定义(环境字符串不同)。这会导致identical
失败。
比较
str(class(cur$assays))
str(cur$assays$getClass())
解决方法是跳过检查属性:expect_equal(target, current, check.attributes = FALSE)
。
作为旁注,这也可以正常工作(请注意,我在这里使用SummarizedExperiment类的getter方法而不是$assays
):
expect_equal(
assays(current),
assays(target)
)