我試圖捕捉一個錯誤,并對特定的錯誤物件進行單元測驗檢查,這里是我測驗的prod函式:
我試圖捕捉一個錯誤,并對特定的錯誤物件進行單元測驗檢查。
const command = "/tmp/cmd"
type RedisAdapter struct {
modeStatus bool {
}
func (r RedisAdapter) Enable(客戶域。 client) error {
cmdArgs := []string{
"redis-job-shard-pause"。
strconv.Itoa(client.Shard())。
strconv.Itoa(client.Duration())。
}
cmd := fmt.Sprintf("%v"/span>, command)
out, err := exec.Command(cmd, cmdArgs...).Output()
fmt.Printf("%v", string(out))
return err
}
當error=nil和error=fs.PathError時,運行一個測驗的Unittest代碼:
func TestEnableService_SetEnable(t *testing.T) {
type fields struct {
LoadEnablePort out.EnablePort
}
type args struct {
客戶端 domain.Client
}
fileNotFound := fs.PathError{
Op: "fork/exec"。
路徑。"/tmp/cmd"。
Err: syscall.ENOENT,
}
tests := []struct {
name string string
fields fields
args args
希望錯誤
}{
{
"啟用Redis"。
欄位{
LoadEnablePort: redis.RedisAdapter{},
},
args{
client: domain.Client{},
},
nil。
},
{
"enable_redis_cmd_not_found"。
欄位{
LoadEnablePort: redis.RedisAdapter{},
},
args{
client: domain.Client{},
},
&fileNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := EnableService{
LoadEnablePort: tt.fields.LoadEnablePort,
}
if got := s.LoadEnablePort.Enable(tt.args.client); got != tt.want {
t.Errorf("LoadEnablePort.RedisAdapter.Enable = %v, want %v"/span>, got, tt.want)
}
})
}
}
nil測驗作業正常,但當我抓到第二個測驗時,即使物件在除錯器中顯示完全相同,也不能說明測驗失敗的原因:
https://i.stack.imgur.com/CTtfk.png
注意 "got "變數與 "tt.want "變數物件完全相同,最后我有以下失敗的測驗:
==== RUN TestEnableService_SetEnable/enable_redis_cmd_not_found
setModeService_test.go:116: LoadEnablePort.RedisAdapter.Enable = fork/exec /tmp/cmd: no such file or directory, want fork/exec /tmp/cmd: no such file or directory
---失敗。TestEnableService_SetEnable/enable_redis_cmd_not_found (549.32s)
非常感謝您的幫助。
uj5u.com熱心網友回復:
這兩個錯誤值是可比的,它們持有指向fs.PathError結構的指標。
他們的地址不同,因此,他們是不一樣的。
如果你對它們進行解參考,它們是相等的。
。
https://play.golang.org/p/ZMoEahbyIX_E 你應該使用 這里指定了平等運算子https://golang.org/ref/spec#Comparison_operators 它說
標籤: 上一篇:QCoreApplication.postEvent(receiver,event)只有在輸入函式時才有好的結果。
下一篇:安卓11存盤訪問和Java檔案庫
errors.Is或者errors.As。...
介面值是可以比較的。
兩個界面值是相等的如果它們有
相同的動態型別和相同的動態值
或者if兩者的值nil。
...
