maraino / go-mock

A mocking framework for the Go Programming Language.
MIT License
35 stars 12 forks source link

When using MockFunction.Call, only the first set of return values is returned #12

Closed telyn closed 6 years ago

telyn commented 6 years ago

The example program below will print "hellohello" instead of "helloworld" - this is because the MockFunction's ReturnValues are not reset between calls. That makes sense when using When and Return, but not when using Call.

package main

import (
    "fmt"

    mock "github.com/maraino/go-mock"
)

type myMock struct {
    mock.Mock
}

func (m myMock) Fn() string {
    r := m.Called()
    return r.String(0)
}

func main() {
    m := myMock{}
    i := 0
    vals := []string{"hello", "world"}
    m.When("Fn").Call(func() string {
        str := vals[i]
        i += 1
        return str
    })
    fmt.Print(m.Fn(), m.Fn())
}
maraino commented 6 years ago

Thanks @telyn for noticing this. It's fixed.