* Bagas Sanjaya <bagasdo...@gmail.com> [220901 06:04]:
> 
> ```
> package tester
> 
> import (
>       "testing"
> )
> 
> type Tests struct {
>       err error
> }
> 
> func TestErr (t *testing.T) {
>       cases := Tests{eTest,}
                       ^
This is at least one of the problems.  You must create an instance of
eTest:

        cases := Tests{&eTest{"what?"}}

This will compile, but the test will fail because actual points to a
different instance of eTest than the one in Tests.

> 
>       t.Run("now testing", func (t *testing.T) {
>               actual := Err()

Depending on what you intend, you can fix this one of two ways.  You can
compare the output of the Error methods:

                if actual.Error() != cases.err.Error() {

or you can create a global  eWhat = &eTest{"what?"}  and then use eWhat
both in the definition of Err above and in the initializer for Tests.

>               if actual != cases.err {
>                       t.Errorf("eh")
>               }
>       })
> }
> ```
> 
> But when I `go test`, I got build error:
> 
> ```
> # test-error [test-error.test]
> ./test-error_test.go:12:17: eTest (type) is not an expression
> ```
> 
> What was wrong above?

There is also a third way:  don't use a pointer receiver on

func (e eTest) Error() string {...}.

How to fix it will depend on how you intend to use eTest in your real
(non-testing) code.  Personally, if my error type needed only the
string, and not any additional fields that you have not shown because
you were simplifying your example, I would not use a struct, but a
string:

type eTest string

func (e eTest) Error() string {
        return fmt.Sprintf("e? %s", e)
}

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/YxCbTehAyvzOuhRc%40basil.wdw.

Reply via email to