context: refactorize assert functions
This commit is contained in:
parent
0b181ccf0f
commit
c0db9ab22b
@ -11,6 +11,7 @@ import (
|
||||
type SpyStore struct {
|
||||
response string
|
||||
cancelled bool
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (s *SpyStore) Fetch() string {
|
||||
@ -22,10 +23,24 @@ func (s *SpyStore) Cancel() {
|
||||
s.cancelled = true
|
||||
}
|
||||
|
||||
func (s *SpyStore) assertWasCancelled() {
|
||||
s.t.Helper()
|
||||
if !s.cancelled {
|
||||
s.t.Error("store was not told to cancel")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SpyStore) assertWasNotCancelled() {
|
||||
s.t.Helper()
|
||||
if s.cancelled {
|
||||
s.t.Error("it should not have cancelled the store")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer(t *testing.T) {
|
||||
t.Run("basic get store", func(t *testing.T) {
|
||||
data := "hello, world"
|
||||
store := &SpyStore{response: data}
|
||||
store := &SpyStore{response: data, t: t}
|
||||
srv := Server(store)
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
@ -37,14 +52,12 @@ func TestServer(t *testing.T) {
|
||||
t.Errorf(`got "%s", want "%s"`, response.Body.String(), data)
|
||||
}
|
||||
|
||||
if store.cancelled {
|
||||
t.Error("it should not have cancelled the store")
|
||||
}
|
||||
store.assertWasNotCancelled()
|
||||
})
|
||||
|
||||
t.Run("tells store to cancel work if request is cancelled", func(t *testing.T) {
|
||||
data := "hello, world"
|
||||
store := &SpyStore{response: data}
|
||||
store := &SpyStore{response: data, t: t}
|
||||
srv := Server(store)
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
@ -59,8 +72,6 @@ func TestServer(t *testing.T) {
|
||||
|
||||
srv.ServeHTTP(response, request)
|
||||
|
||||
if !store.cancelled {
|
||||
t.Error("store was not told to cancel")
|
||||
}
|
||||
store.assertWasCancelled()
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user