Skip to content

Commit

Permalink
fix tests to fail when expected error is not returned
Browse files Browse the repository at this point in the history
Signed-off-by: Kelly Deng <[email protected]>
  • Loading branch information
kqdeng committed Aug 19, 2020
1 parent 43d14f4 commit 91c9f84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 11 additions & 9 deletions grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func TestGetByIPCacher(t *testing.T) {
}
hw := exportedHardwareCacher{}
err = json.Unmarshal(ehw, &hw)
if err != nil {
if err.Error() != test.error {
t.Fatalf("unexpected error while unmarshalling, want: %v, got: %v\n", test.error, err.Error())
if test.error != "" {
if err == nil {
t.Fatalf("unmarshal should have returned error: %v", test.error)
} else if err.Error() != test.error {
t.Fatalf("unmarshal returned wrong error, want: %v, got: %v\n", err, test.error)
}
continue
}

if hw.State != test.state {
Expand Down Expand Up @@ -86,11 +87,12 @@ func TestGetByIPTinkerbell(t *testing.T) {
hardwareClient: hardwareGetterMock{test.json},
}
ehw, err := getByIP(context.Background(), hegelTestServer, mockUserIP) // returns hardware data as []byte
if err != nil {
if err.Error() != test.error {
t.Fatalf("unexpected error in getByIP, want: %v, got: %v\n", test.error, err.Error())
if test.error != "" {
if err == nil {
t.Fatalf("getByIP should have returned error: %v", test.error)
} else if err.Error() != test.error {
t.Fatalf("getByIP returned wrong error: got %v want %v", err, test.error)
}
continue
}

hw := struct {
Expand All @@ -99,7 +101,7 @@ func TestGetByIPTinkerbell(t *testing.T) {
}{}
err = json.Unmarshal(ehw, &hw)
if err != nil {
t.Error("Error in unmarshalling hardware metadata", err)
t.Error("error in unmarshalling hardware metadata", err)
}

if hw.ID != test.id {
Expand Down
8 changes: 6 additions & 2 deletions http_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@ func TestRegisterEndpoints(t *testing.T) {
http.DefaultServeMux = &http.ServeMux{} // reset registered patterns

err := registerCustomEndpoints()
if err != nil && err.Error() != test.error {
t.Fatalf("unexpected error: got %v want %v", err, test.error)
if test.error != "" {
if err == nil {
t.Fatalf("registerCustomEndpoints should have returned error: %v", test.error)
} else if err.Error() != test.error {
t.Fatalf("registerCustomEndpoints returned wrong error: got %v want %v", err, test.error)
}
}

req, err := http.NewRequest("GET", test.url, nil)
Expand Down

0 comments on commit 91c9f84

Please sign in to comment.