1

I'm writing unit tests, and I want to write a unit test that asserts that a public method on the struct (Foo.Start) properly handles error responses from an internal method on the struct (Foo.internal).

Essentially I want to get test coverage on the this section of my code:

if err != nil {
    return err
}

Here's an example of code and associated test that doesn't work (but would work in say, Python)

# example.go

package example

import "fmt"

type FooAPI interface {
    Start() error
    internal(string) (string, error)
}

type Foo struct {
    FooAPI
}

func (foo Foo) Start() (err error) {

    data, err := foo.internal("bar")
    if err != nil {
        return err
    }

    fmt.Println(data)
    return err
}

func (foo Foo) internal(input string) (output string, err error) {
    return output, err
}
# example_test.go

package example

import (
    "testing"

    "github.com/pkg/errors"
)

type MockFoo struct {
    FooAPI
}

func (foo MockFoo) internal(input string) (output string, err error) {
    return output, errors.New("big error")
}

func TestStart(t *testing.T) {
    tdata := []struct {
        testCase        string
        expectedAnError bool
        foo             FooAPI
    }{
        {
            testCase:        "standard_case",
            expectedAnError: false,
            foo:             Foo{},
        },
        {
            testCase:        "error_case",
            expectedAnError: true,
            foo:             MockFoo{},
        },
    }
    for _, test := range tdata {
        t.Run(test.testCase, func(t *testing.T) {
            // The function under test
            test.foo.Start = Foo.Start // <= this doesn't work
            err := test.foo.Start()

            // Assertion 1
            if test.expectedAnError == false && err != nil {
                t.Error(err.Error())
            }

            // Assertion 2
            if test.expectedAnError == true && err == nil {
                t.Error("expected an error, but there was none!")
            }
        })
    }
}

I'm less interested in correcting the specific example, more-so my goal is to get test coverage on Foo.Start's error handling. I feel like there's some trick with interfaces or pointers that will get me across the finish line here?

kai
  • 1,288
  • 3
  • 12
  • 24

1 Answers1

1

I figured out one solution, inspired by https://stackoverflow.com/a/48206430/3055558

Using a internal{{ your struct }} struct and associated interface, and mocking that.

# example.go

package example

import "fmt"

type internalFooAPI interface {
    internalBehavior(string) (string, error)
}

type Foo struct {
    internal internalFooAPI
}

type internalFoo struct{}

func NewFoo(internal internalFooAPI) Foo {
    return Foo{
        internal: internal,
    }
}

func (foo Foo) Start() (err error) {

    data, err := foo.internal.internalBehavior("bar")
    if err != nil {
        return err
    }

    fmt.Println(data)
    return err
}

func (foo internalFoo) internalBehavior(input string) (output string, err error) {
    return output, err
}
# example_test.go

package example

import (
    "testing"

    "github.com/pkg/errors"
)

type mockInternalFoo struct{}

type mockInternalFooWithErrors struct{}

func (foo mockInternalFoo) internalBehavior(input string) (output string, err error) {
    return output, err
}

func (foo mockInternalFooWithErrors) internalBehavior(input string) (output string, err error) {
    return output, errors.New("big error")
}

func TestStart(t *testing.T) {
    tdata := []struct {
        testCase        string
        expectedAnError bool
        foo             Foo
    }{
        {
            testCase:        "standard_case",
            expectedAnError: false,
            foo:             NewFoo(internalFoo{}),
        },
        {
            testCase:        "mock_case",
            expectedAnError: false,
            foo:             NewFoo(mockInternalFoo{}),
        },
        {
            testCase:        "error_case",
            expectedAnError: true,
            foo:             NewFoo(mockInternalFooWithErrors{}),
        },
    }
    for _, test := range tdata {
        t.Run(test.testCase, func(t *testing.T) {
            // The function under test
            err := test.foo.Start()

            // Assertion 1
            if test.expectedAnError == false && err != nil {
                t.Error(err.Error())
            }

            // Assertion 2
            if test.expectedAnError == true && err == nil {
                t.Error("expected an error, but there was none!")
            }
        })
    }
}
kai
  • 1,288
  • 3
  • 12
  • 24