Original post

So I got this method (run):

“` func main() { s := MyStruct{MyInterface: Implementation{}} s.run() }

type DoingInterface interface { doThis() }

type MyStruct struct { MyInterface DoingInterface }

func (a MyStruct) run() { a.MyInterface.doThis() }

type Implementation struct { }

func (i Implementation) doThis() { fmt.Println(“doing it”) } “`

I wanna test it. In order to test if the method doThis() is correctly called, I mock the interface implementation like:

“` type MockedImplementation struct { done bool }

func (m MockedImplementation) doThis() { m.done = true }

func TestStructRun(t *testing.T) { mock := MockedImplementation{} var s = MyStruct{MyInterface: mock} s.run()

var m = s.MyInterface.(MockedImplementation) if !m.done { t.Error(“it’s not done!”) } } “`

Why the test fails? How this should be done? Thanks in advance.

submitted by /u/aw4y_z
[link] [comments]