亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復原始字符串文字上的失敗斷言

如何修復原始字符串文字上的失敗斷言

Go
當年話下 2023-06-01 09:56:59
我正在為一個寧靜的 api 編寫單元測試,并希望確保得到預期的響應。如何去掉預期字符串文字末尾的“\n”?我正在使用 stethr 的 testify 包。我曾嘗試使用字符串 TrimSuffix、TrimRight 函數,但沒有成功。func TestGetConfig(t *testing.T) {    testServer := initTestServer(t)    req, err := http.NewRequest("GET", "/api/config", nil)    if err != nil {    t.Fatal(err)    }    rr := httptest.NewRecorder()    handler := http.HandlerFunc(testServer.getConfig)    handler.ServeHTTP(rr, req)    //Check the status code is what we expect    if status := rr.Code; status != http.StatusOK {    t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)    }    //Check the response body is what we expect. expected := `{"domain":"","ip":"","redirect_key":"mj","redirect_url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ","verification_key":"yp","verification_token":"5a62"}`    expected = strings.TrimSuffix(expected, "\n")    assert.Equal(t, rr.Body.String(), expected)}我希望測試能夠通過,但它卻失敗了,并將其作為輸出。Error Trace:    config_test.go:94                    Error:          Not equal:                                    expected: "{\"domain\":\"\",\"ip\":\"\",\"redirect_key\":\"mj\",\"redirect_url\":\"https://www.youtube.com/watch?v=dQw4w9WgXcQ\",\"verification_key\":\"yp\",\"verification_token\":\"5a62\"}\n"                                    actual  : "{\"domain\":\"\",\"ip\":\"\",\"redirect_key\":\"mj\",\"redirect_url\":\"https://www.youtube.com/watch?v=dQw4w9WgXcQ\",\"verification_key\":\"yp\",\"verification_token\":\"5a62\"}"
查看完整描述

2 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

您正在從字符串而不是實際的響應正文中刪除一個不存在的"\n"字符。expected

"\n"但更簡單的方法是只在字符串中包含expected。這樣,預期的字符串實際上就是您所期望的。


查看完整回答
反對 回復 2023-06-01
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

參數順序錯誤。

這是

assert.Equal(t,?rr.Body.String(),?expected)

它應該是

assert.Equal(t,?expected,?rr.Body.String())
查看完整回答
反對 回復 2023-06-01
  • 2 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號