Skip to content

Commit 6e59f20

Browse files
committed
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
1 parent 840f800 commit 6e59f20

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

assert/assertions_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,10 @@ func (m *mockTestingT) Errorf(format string, args ...interface{}) {
25142514
m.args = args
25152515
}
25162516

2517+
func (m *mockTestingT) Failed() bool {
2518+
return m.errorFmt != ""
2519+
}
2520+
25172521
func TestFailNowWithPlainTestingT(t *testing.T) {
25182522
mockT := &mockTestingT{}
25192523

assert/http_assertions_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ func TestHTTPSuccess(t *testing.T) {
4141
assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false)
4242
assert.True(mockT2.Failed())
4343

44-
mockT3 := new(testing.T)
44+
mockT3 := new(mockTestingT)
4545
assert.Equal(HTTPSuccess(
4646
mockT3, httpError, "GET", "/", nil,
4747
"was not expecting a failure here",
4848
), false)
4949
assert.True(mockT3.Failed())
50+
assert.Contains(mockT3.errorString(), "was not expecting a failure here")
5051

5152
mockT4 := new(testing.T)
5253
assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false)
@@ -60,12 +61,13 @@ func TestHTTPSuccess(t *testing.T) {
6061
func TestHTTPRedirect(t *testing.T) {
6162
assert := New(t)
6263

63-
mockT1 := new(testing.T)
64+
mockT1 := new(mockTestingT)
6465
assert.Equal(HTTPRedirect(
6566
mockT1, httpOK, "GET", "/", nil,
6667
"was expecting a 3xx status code. Got 200.",
6768
), false)
6869
assert.True(mockT1.Failed())
70+
assert.Contains(mockT1.errorString(), "was expecting a 3xx status code. Got 200.")
6971

7072
mockT2 := new(testing.T)
7173
assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true)
@@ -87,12 +89,13 @@ func TestHTTPError(t *testing.T) {
8789
assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false)
8890
assert.True(mockT1.Failed())
8991

90-
mockT2 := new(testing.T)
92+
mockT2 := new(mockTestingT)
9193
assert.Equal(HTTPError(
9294
mockT2, httpRedirect, "GET", "/", nil,
9395
"Expected this request to error out. But it didn't",
9496
), false)
9597
assert.True(mockT2.Failed())
98+
assert.Contains(mockT2.errorString(), "Expected this request to error out. But it didn't")
9699

97100
mockT3 := new(testing.T)
98101
assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true)
@@ -114,12 +117,13 @@ func TestHTTPStatusCode(t *testing.T) {
114117
assert.Equal(HTTPStatusCode(mockT2, httpRedirect, "GET", "/", nil, http.StatusSwitchingProtocols), false)
115118
assert.True(mockT2.Failed())
116119

117-
mockT3 := new(testing.T)
120+
mockT3 := new(mockTestingT)
118121
assert.Equal(HTTPStatusCode(
119122
mockT3, httpError, "GET", "/", nil, http.StatusSwitchingProtocols,
120123
"Expected the status code to be %d", http.StatusSwitchingProtocols,
121124
), false)
122125
assert.True(mockT3.Failed())
126+
assert.Contains(mockT3.errorString(), "Expected the status code to be 101")
123127

124128
mockT4 := new(testing.T)
125129
assert.Equal(HTTPStatusCode(mockT4, httpStatusCode, "GET", "/", nil, http.StatusSwitchingProtocols), true)
@@ -179,7 +183,7 @@ func TestHTTPRequestWithParams(t *testing.T) {
179183

180184
func TestHttpBody(t *testing.T) {
181185
assert := New(t)
182-
mockT := new(testing.T)
186+
mockT := new(mockTestingT)
183187

184188
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
185189
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
@@ -191,6 +195,7 @@ func TestHttpBody(t *testing.T) {
191195
"Expected the request body to not contain 'World'. But it did.",
192196
))
193197
assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
198+
assert.Contains(mockT.errorString(), "Expected the request body to not contain 'World'. But it did.")
194199

195200
assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello"))
196201
}

0 commit comments

Comments
 (0)