Creating a test case for error checking

Report a typo

In this task, you will try to check that a function returns the error you need. It is "division by zero".

For your convenience, you have the code with the necessary operations, you just need to fill in the gaps. If everything is correct, the system will display OK.

Sample Input 1:

Sample Output 1:

OK
Write a program in Go
package main

import (
"errors"
"fmt"
"testing"
)

var ErrDivByZero = errors.New("division by zero")
var Div func(a, b int) (int, error) // nolint: gochecknoglobals // DO NOT delete this comment!

func TestDivByZero(t *testing.T) {
// replace all "_"
_, _ := _
if err != nil && !errors._(err, ErrDivByZero) {
t.Errorf("got != expected: %v", err)
}
}

// DO NOT MODIFY
func main() {
var t testing.T

Div = func(a, b int) (int, error) {
if b == 0 {
return 0, ErrDivByZero
}
fmt.Println("b != 0: in this task you only need to check the case when b == 0")
return 0, nil
}

TestDivByZero(&t)

fmt.Println("OK")
}
___

Create a free account to access the full topic