vendredi 2 novembre 2018

Go, how to mock a method of a struct that calls another method of the same struct

I need a little help with mocking sturct member functions in Go.

Here is the specific use case:

I have a struct Foo with a field which represents some internal state of the struct instance. This struct also has two member functions Bar() and Baz(). I want to test Baz() while mocking out Bar().

Thank you for your help.

package awesomeProject

import "fmt"

type Foo struct {
    state string
}

func NewFoo(s string) *Foo {
    return &Foo{s}
}

func (f *Foo) Bar() {
    fmt.Println(f.state)
}

func (f *Foo) Baz() {
    if len(f.state) > 0 {
        f.Bar()
    }
}

Aucun commentaire:

Enregistrer un commentaire