我有一个抛出一个对象的函数,我怎么能断言正确的对象被抛出?
it('should throw', () => {
const errorObj = {
myError: {
name: 'myError',
desc: 'myDescription'
}
};
const fn = () => {
throw errorObj;
}
expect(() => fn()).toThrowError(errorObj);
});
答案 0 :(得分:5)
如果您要测试自定义错误的内容(我认为您正在尝试这样做)。您可以捕获错误然后执行断言。
it('should throw', () => {
let thrownError;
try {
fn();
}
catch(error) {
thrownError = error;
}
expect(thrownError).toEqual(expectedErrorObj);
});
正如Dez所说,如果你不抛出一个javascript Error对象的实例,那么toThrowError函数将不起作用。但是,您可以通过装饰错误对象的实例来创建自定义错误。
e.g。
let myError = new Error('some message');
myError.data = { name: 'myError',
desc: 'myDescription' };
throw myError;
然后,一旦您在测试中发现错误,您就可以测试错误的自定义内容。
expect(thrownError.data).toEqual({ name: 'myError',
desc: 'myDescription' });
答案 1 :(得分:1)
您需要抛出一个Javascript Option Explicit
Sub CompletionStatusUpdate()
Dim CompletionStatus As Range
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws1 As Worksheet
Set ws1 = wb.Sheets("One")
Dim ws2 As Worksheet
Set ws2 = wb.Sheets("Two")
Dim h9Range As Range
Set h9Range = ws1.Range("H9")
'Test for h9Range being not empty and that is greater than 2?
ws2.Range("H2:H" & h9Range.Value + 1).Interior.ColorIndex = 0 'if this is intended to clear prior runs it needs + 1
ws2.Range("H2:H" & h9Range.Value + 1).ClearContents 'This seems to be the same as loopRange?
Dim completeRange As Range
Set completeRange = ws1.Range("H11")
Dim missingRange As Range
Set missingRange = ws1.Range("H13")
missingRange.ClearContents
completeRange.ClearContents
Dim h8Range As Range
Set h8Range = ws1.Range("H8")
Dim dRange As Range
Set dRange = ws2.Range("D" & h9Range.Value)
Dim fRange As Range
Set fRange = ws2.Range("F" & h9Range.Value)
Dim countRange As Range
Set countRange = ws2.Range("H2:H" & h9Range.Value + 1)
Dim h6Range As Range
Set h6Range = ws1.Range("H6")
Dim loopRange As Range
Set loopRange = ws2.Range("H2:H" & h9Range.Value + 1).SpecialCells(xlCellTypeVisible)
DataGetCompletion = 3
If dRange = "Yes" And fRange = "Yes" Then
h8Range = 1
ElseIf dRange = "No" And fRange = "No" Then
h8Range = 2
Else
h8Range = 3
End If
For Each CompletionStatus In loopRange
If CompletionStatus.EntireRow.Hidden = False Then
Select Case h8Range
Case 1
CompletionStatus.Interior.ColorIndex = 4
CompletionStatus.Value = "Both data sets complete"
Case 2
CompletionStatus.Interior.ColorIndex = 3
CompletionStatus.Value = "Both data sets missing"
End Select
End If
Next CompletionStatus
completeRange = Application.WorksheetFunction.CountIf _
(countRange, "Both data sets complete")
missingRange = Application.WorksheetFunction.CountIf _
(countRange, "Both data sets missing")
End Sub
对象,因此Jest Error
方法会识别出已抛出错误。此外,toThrowError
看起来与所引发错误的消息相匹配,或者如果您只是通过toThrowError
进行检查,则会抛出错误。
.toThrowError()
如果要检查整个对象是否按原样传递,则需要像这样检查:
it('should throw', () => {
const errorObj = {
myError: {
name: 'myError',
desc: 'myDescription'
}
};
const fn = () => {
throw new Error(errorObj.myError.desc);
}
expect(() => fn()).toThrowError("myDescription");
});
答案 2 :(得分:1)
这是开玩笑的已知问题,请参见https://github.com/facebook/jest/issues/8140
同时,这是我的解决方法-https://github.com/DanielHreben/jest-matcher-specific-error