http_assertions: assert that the msgAndArgs actually works in tests

This commit also adds the method `Failed() bool` to the mockTestingT
struct. This is usefull for asserting failure in tests
pull/1548/head
Arjun Mahishi 2024-02-23 22:41:24 +05:30
parent 840f800f1a
commit 6e59f20c0d
2 changed files with 14 additions and 5 deletions

View File

@ -2514,6 +2514,10 @@ func (m *mockTestingT) Errorf(format string, args ...interface{}) {
m.args = args m.args = args
} }
func (m *mockTestingT) Failed() bool {
return m.errorFmt != ""
}
func TestFailNowWithPlainTestingT(t *testing.T) { func TestFailNowWithPlainTestingT(t *testing.T) {
mockT := &mockTestingT{} mockT := &mockTestingT{}

View File

@ -41,12 +41,13 @@ func TestHTTPSuccess(t *testing.T) {
assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false) assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false)
assert.True(mockT2.Failed()) assert.True(mockT2.Failed())
mockT3 := new(testing.T) mockT3 := new(mockTestingT)
assert.Equal(HTTPSuccess( assert.Equal(HTTPSuccess(
mockT3, httpError, "GET", "/", nil, mockT3, httpError, "GET", "/", nil,
"was not expecting a failure here", "was not expecting a failure here",
), false) ), false)
assert.True(mockT3.Failed()) assert.True(mockT3.Failed())
assert.Contains(mockT3.errorString(), "was not expecting a failure here")
mockT4 := new(testing.T) mockT4 := new(testing.T)
assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false) assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false)
@ -60,12 +61,13 @@ func TestHTTPSuccess(t *testing.T) {
func TestHTTPRedirect(t *testing.T) { func TestHTTPRedirect(t *testing.T) {
assert := New(t) assert := New(t)
mockT1 := new(testing.T) mockT1 := new(mockTestingT)
assert.Equal(HTTPRedirect( assert.Equal(HTTPRedirect(
mockT1, httpOK, "GET", "/", nil, mockT1, httpOK, "GET", "/", nil,
"was expecting a 3xx status code. Got 200.", "was expecting a 3xx status code. Got 200.",
), false) ), false)
assert.True(mockT1.Failed()) assert.True(mockT1.Failed())
assert.Contains(mockT1.errorString(), "was expecting a 3xx status code. Got 200.")
mockT2 := new(testing.T) mockT2 := new(testing.T)
assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true) assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true)
@ -87,12 +89,13 @@ func TestHTTPError(t *testing.T) {
assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false) assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false)
assert.True(mockT1.Failed()) assert.True(mockT1.Failed())
mockT2 := new(testing.T) mockT2 := new(mockTestingT)
assert.Equal(HTTPError( assert.Equal(HTTPError(
mockT2, httpRedirect, "GET", "/", nil, mockT2, httpRedirect, "GET", "/", nil,
"Expected this request to error out. But it didn't", "Expected this request to error out. But it didn't",
), false) ), false)
assert.True(mockT2.Failed()) assert.True(mockT2.Failed())
assert.Contains(mockT2.errorString(), "Expected this request to error out. But it didn't")
mockT3 := new(testing.T) mockT3 := new(testing.T)
assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true) assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true)
@ -114,12 +117,13 @@ func TestHTTPStatusCode(t *testing.T) {
assert.Equal(HTTPStatusCode(mockT2, httpRedirect, "GET", "/", nil, http.StatusSwitchingProtocols), false) assert.Equal(HTTPStatusCode(mockT2, httpRedirect, "GET", "/", nil, http.StatusSwitchingProtocols), false)
assert.True(mockT2.Failed()) assert.True(mockT2.Failed())
mockT3 := new(testing.T) mockT3 := new(mockTestingT)
assert.Equal(HTTPStatusCode( assert.Equal(HTTPStatusCode(
mockT3, httpError, "GET", "/", nil, http.StatusSwitchingProtocols, mockT3, httpError, "GET", "/", nil, http.StatusSwitchingProtocols,
"Expected the status code to be %d", http.StatusSwitchingProtocols, "Expected the status code to be %d", http.StatusSwitchingProtocols,
), false) ), false)
assert.True(mockT3.Failed()) assert.True(mockT3.Failed())
assert.Contains(mockT3.errorString(), "Expected the status code to be 101")
mockT4 := new(testing.T) mockT4 := new(testing.T)
assert.Equal(HTTPStatusCode(mockT4, httpStatusCode, "GET", "/", nil, http.StatusSwitchingProtocols), true) assert.Equal(HTTPStatusCode(mockT4, httpStatusCode, "GET", "/", nil, http.StatusSwitchingProtocols), true)
@ -179,7 +183,7 @@ func TestHTTPRequestWithParams(t *testing.T) {
func TestHttpBody(t *testing.T) { func TestHttpBody(t *testing.T) {
assert := New(t) assert := New(t)
mockT := new(testing.T) mockT := new(mockTestingT)
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")) assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World")) assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
@ -191,6 +195,7 @@ func TestHttpBody(t *testing.T) {
"Expected the request body to not contain 'World'. But it did.", "Expected the request body to not contain 'World'. But it did.",
)) ))
assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world")) assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
assert.Contains(mockT.errorString(), "Expected the request body to not contain 'World'. But it did.")
assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello")) assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello"))
} }