我正在为一个宁静的 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\"}"
幕布斯6054654
守候你守候我
相关分类