我正在写一个测试脚本,以测试是否根据我的逻辑做了一些修改。我期望的输出和实际输出是json文件,它们是完全相同的。我的目的是检查实际输出是否等于预期输出。首先,我的功能是它,它必须从该位置读取json文件,并且必须将parentTCode字符替换为null,因为我们只需要parentTCode中的数字。
LoadData = function(inputPath) {
options(encoding = "UTF-8")
if(file.exists(inputPath)) {
setting = read_json(path = inputPath, simplifyVector = TRUE)
setting$ParentTCode = stri_replace(str = setting$ParentTCode, replacement = "", fixed = "T")
} else {
stop(paste0("There's no setting json file at ", inputPath))
}
return(setting)
}
我的测试脚本是这样
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./Test/Data/Input/setting.json"
expected_output = "./Test/Data/expected_output.json"
expect_equal(LoadData(inputPath = filePath), expected_output)
}
)
我感到困惑,因为当实际输出和预期输出相同时,为什么会引发这样的错误。
Error: LoadSettingJsonLDG(inputPath = filePath) not equal to `expected_output`.
Modes: list, character
names for target but not for current
Length mismatch: comparison on first 1 components
Component 1: 1 string mismatch
我将在这里附加我的json文件示例,如下所示
{
"ParentTCode": ["T2802"],
"Code": ["0001"],
"DataType": ["Diva"],
"FileExtention": [null],
"Currency": [false],
"OriginalUnit": [1000],
"ExportUnit": [1000000]
}
这是LoadData函数的输入文件。输出看起来像这样
{
"ParentTCode": ["2802"],
"Code": ["0001"],
"DataType": ["Diva"],
"FileExtention": [null],
"Currency": [false],
"OriginalUnit": [1000],
"ExportUnit": [1000000]
}
如果有人能帮助我,我会很高兴。提前致谢。
expect_equal
调用的第二个参数是character
长度1,它是指向包含所需输出内容的文件的路径。由于第一个参数是a list
,所以acharacter
和list
不相等也就不足为奇了。
我认为您打算将该文件的解析内容进行比较。如果您将测试替换为:
test_that(desc = "Test for 'LoadData' Condition 1",
code = {
filePath = "./Test/Data/Input/setting.json"
expected_output = "./Test/Data/expected_output.json"
expect_equal(LoadData(inputPath = filePath),
fromJSON(expected_output))
}
)
它应该工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句