mockery icon indicating copy to clipboard operation
mockery copied to clipboard

How to mock ... args ?

Open zacscoding opened this issue 4 years ago • 1 comments

Hello, I want to setup and assert of ... parameters in function.

for example, we have below interface and generated mock

// my interface
//go:generate mockery
type Member interface {
	GetMembers(name ...string)
}

// in test, where Mock is generated from above interface
Mock.On("GetMembers", mock.AnythingOfType("????"))

then, is it possible to use mock.Anything or others regardless of argument numbers?

zacscoding avatar Feb 19 '21 05:02 zacscoding

You'll have to know the exact number of expected arguments.

Matching is done like this:

// expected call
member.GetMembers("one")
// mock
memberMock.On("GetMembers", "one")

// With several arguments
members.GetMembers("one", "two")
// you have to provide the same number of arguments
memberMock.On("GetMembers", "one", mock.AnythingOfType("string"))

mblottiere avatar Jul 22 '21 14:07 mblottiere

@zacscoding you could try my fork https://github.com/Laisky/mockery

I add .Catch to catch by name with any arguments

Laisky avatar Jan 30 '23 09:01 Laisky

How to handle this is described in https://vektra.github.io/mockery/notes/#variadic-arguments

Closing this ticket, but feel free to open another one (or just comment back here) if something is not clear.

LandonTClipp avatar Apr 06 '23 21:04 LandonTClipp