[英]testthat 'expect_equal' returns an error - it isn't true though the actual output and exepected outputs are the same
我正在編寫一個測試腳本來測試是否根據我的邏輯進行了一些修改。 我預期的 output 和實際的 output 是 json 文件,它們完全相同。 我的目的是檢查實際的 output 是否等於預期的 output。 首先我的 function 是這個,它必須從該位置讀取 json 文件,它必須用 null 替換 parentTCode 字符,因為我們只需要 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 function 的輸入文件。 output 看起來像這樣
{
"ParentTCode": ["2802"],
"Code": ["0001"],
"DataType": ["Diva"],
"FileExtention": [null],
"Currency": [false],
"OriginalUnit": [1000],
"ExportUnit": [1000000]
}
如果有人可以幫助我,我會很高興。 提前致謝。
expect_equal
調用的第二個參數是character
,長度為 1,它是指向包含您期望 output 的文件的路徑。 由於第一個參數是list
,因此character
和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))
}
)
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.