Skip to content

Commit

Permalink
added t.Helper() to show failing test instead of httpmocklib
Browse files Browse the repository at this point in the history
  • Loading branch information
Sapexoid committed Feb 20, 2023
1 parent 0350c5c commit 8724b63
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions httpmockserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ func New(t T) MockServer {
}

type T interface {
Helper()
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}

// NewWithOpts can be used to create a mock server with custom options
func NewWithOpts(t T, opts Opts) MockServer {
t.Helper()
err := opts.validate()
if err != nil {
t.Fatalf("invalid options: %v", err)
Expand Down Expand Up @@ -140,6 +142,7 @@ func (s *mockServer) BaseURL() string {
}

func (s *mockServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.t.Helper()
s.handlerMutex.Lock()
defer s.handlerMutex.Unlock()

Expand Down Expand Up @@ -259,6 +262,7 @@ func (s *mockServer) DEFAULT() RequestExpectation {
}

func (s *mockServer) AssertExpectations() {
s.t.Helper()
s.finisheCalled = true
var buf bytes.Buffer

Expand Down
3 changes: 3 additions & 0 deletions httpmockserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,3 +922,6 @@ func (t *TMock) Fatalf(format string, args ...interface{}) {
func (t *TMock) Errorf(format string, args ...interface{}) {
t.Called(format, args)
}

func (t *TMock) Helper() {
}
2 changes: 1 addition & 1 deletion request_expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (exp *requestExpectation) Custom(validation RequestValidationFunc, descript
}

func (exp *requestExpectation) Response(code int) ResponseExpectation {

exp.t.Helper()
if exp.every {
exp.t.Fatalf("Every is used to check conditions on every request, therefore it cannot be used with Response()")
return nil
Expand Down
1 change: 1 addition & 0 deletions response_expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (exp *responseExpectation) StringBody(body string) ResponseExpectation {
// JsonBody sets the body of the response to the given object (e.g. `{"foo":"bar"}` or map[string]string{"foo":"bar"})
// you may provide a go object or a valid json string
func (exp *responseExpectation) JsonBody(object interface{}) ResponseExpectation {
exp.t.Helper()
if object == nil {
return exp.Body(nil)
}
Expand Down

0 comments on commit 8724b63

Please sign in to comment.