mercredi 8 janvier 2020

Mock Go like Mock in Python

I've been coding in Go recently, and I like the way they do the mocks:

m := NewMockFoo(ctrl)

m.EXPECT().
  Bar(gomock.Eq(99)).
  Return(101)

The nice thing with this line is that the mock will fail if it is called with something other than 99, and return 101 only if 99 is given. It allows then to "bind" the expected value with the returned one.

With python Mock, I find myself doing something like this :

m = Mock(return_value=101)
m(99)
m.assert_called_once_with(99)

But there is no relationship between the 101 and the 99. I need to play with the call order or something to validate if I want to test several calls.

Is there a better way?

Aucun commentaire:

Enregistrer un commentaire