2 回答

TA貢獻1784條經驗 獲得超8個贊
使用類型斷言:
err := someFunc()
if retryable, ok := err.(*RetryableError); ok {
// use retryable
}
你的RetryableError不是錯誤,而是*RetryableError錯誤。糾正:
func (a RetryableError) Error() string {
return a.msg
}

TA貢獻1862條經驗 獲得超7個贊
assert.IsType
來自https://medium.com/@sebdah/go-best-practices-testing-3448165a0e18的片段:
func TestDivision(t *testing.T) {
? ? tests := []struct{
? ? ? ? x? ? ? float64
? ? ? ? y? ? ? float64
? ? ? ? result float64
? ? ? ? err? ? error
? ? }{
? ? ? ? { x: 1.0, y: 2.0, result: 0.5, err: nil },
? ? ? ? { x: -1.0, y: 2.0, result: -0.5, err: nil},
? ? ? ? { x: 1.0, y: 0.0, result: 0.0, err: ErrZeroDivision},
? ? }
? ? for _, test := range tests {
? ? ? ? result, err := divide(test.x, test.y)
? ? ? ? assert.IsType(t, test.err, err)
? ? ? ? assert.Equal(t, test.result, result)
? ? }
}
- 2 回答
- 0 關注
- 166 瀏覽
添加回答
舉報