作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 test_that 进行单元测试,我正在尝试比较两个对象。在某些情况下,无论内容如何,这两个对象似乎永远不会相同( expect_identical
),但如果它们属于同一类,则可以相等( expect_equal
)。我确定这与类的构造方式有关(我正在使用 R.oo)
那么......是否可以 reshape /展平/重组一个对象(可能成为一个列表?)以便提取原始数据,从而使两个对象具有可比性?我意识到这可能是一项不可能完成的任务。我对 R 中的环境知之甚少,无法确定。
编辑
这是一个例子:
library(R.oo)
library(testthat)
setConstructorS3( "MyClass" , function( )
{
extend( Object() , "MyClass" ,
.param1 = rnorm(10)
)
} )
test1 = MyClass()
test2 = MyClass()
expect_identical(test1, test2) # error: Objects equal but not identical
expect_equal( test1, test2) # no error reported
最佳答案
与 str
进行一些互动探索引导我这样做:
as.list.Object <- function(x, ...) {
vars <- as.list(attr(x, ".env"), all = TRUE)
vars[substr(names(vars), 1, 3) != "..."]
}
all.equal.Object <- function(target, current, ...) {
all.equal(as.list(target), as.list(current), ...)
}
expect_equal(test1, test2)
# Error: test1 not equal to test2
# Component 1: Mean relative difference: 1.056427
关于unit-testing - 如何 reshape S3 对象以使其具有可比性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7209885/
我是一名优秀的程序员,十分优秀!