-4

I am trying to mock a function using GoMonkey, but I found it failed because compiler will be inline this function when program compiles, so GoMonkey can't mock it.

So how can I mock this function or how can I mock a other member's private function?

Test Code Like this:

        patches := gomonkey.ApplyFunc(time.ParseInLocation, func(format string, timeStr string, cstSh *time.Location) (time.Time,error) {
            return time.Time{},errors.New("error")
        })
        defer patches.Reset()
        resp:= Example("")
        So(resp, ....)

mock function

...
t, err := time.ParseInLocation(format, timeStr, cstSh)
...
sulingr
  • 1
  • 2

1 Answers1

0

You can prevent the go compiler from inlining functions using the pragma //go:noinline though as per the previous replies I would avoid the use of mocking if it's at all possible. In addition, use of these pragmas is also not a particularly good idea.

Olaf Bogus
  • 26
  • 3