开始-如何模拟需要回调的依赖项?

Suppose I have a dependency that looks like this:

type Dependency interface {
    Retrieve(transform func(row *Row) string) []string
}

And I'm using it in code that I'd like to unit test

// ...
result := dep.Retrieve(func(row *Row) string {
    // ... do stuff
})
// ...

This is a contrived example, but consider it for something like gcloud pubsub, which has a Receive method that calls a user-defined function for each message it pulls.

If I use mockgen to get a mock of Dependency, how do I tell the mock how to behave? I'd want it to call its input function some number of times with some sets of input.

Here is some code while trying to understand what you wanted to achieve. I don't use mockgen, but manually, you can do what you want. I totally changed the behavior between the original and the mocked call. Does it answer your questions?

package main

import (
    "fmt"
)

var str = [...]string{
    "world",
    "night",
}

type Dependency interface {
    Get(val string) string
}

type dependency struct {
    i int
}

func (d *dependency) Get(fn func(int) string) string {
    d.i++
    d.i = d.i % len(str)

    return "hello " + fn(d.i)
}

func main() {
    d := dependency{}
    myfn := func(idx int) string { return str[idx] }
    fmt.Println(d.Get(myfn))
    fmt.Println(d.Get(myfn))
    fmt.Println(d.Get(myfn))

    m := mock{}
    fmt.Println(m.Get(myfn))
}

type mock struct {
}

func (m *mock) Get(fn func(int) string) string {
    i := 0
    j := (i + 1) % len(str)
    k := (j + 1) % len(str)

    return "mocked " + fn(i) + fn(j) + fn(k)
}

The play link: https://play.golang.org/p/bb7WrmlIEN