stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.52k stars 1.56k forks source link

mockCall.Unset cause panic #1236

Closed hieunguyen1809 closed 1 year ago

hieunguyen1809 commented 1 year ago

Hi team, I'm using testify to testing my method but encounter this issue (It only occur if using matchedBy arg) When I using mock following by example but got error when using mockCall.Unset(). playground: https://go.dev/play/p/oQ2sGSRCtbR

lisitsky commented 1 year ago

I've got the same trouble :( Could you check and may be vote for my fix for this? https://github.com/stretchr/testify/pull/1250

at-syot commented 1 year ago

For people who face this issue, i use this code

    unset := func(call *mock.Call) {
        if call != nil {
            call.Parent.ExpectedCalls = []*mock.Call{}
        }
    }
    var caller *mock.Call
    unset(caller)

as temporary fix. you can try this to

gordonlee commented 1 year ago

Just fyi, I put short snippet which only specific method would be deleted based on @at-syot 's code

var unsetFn = func(call *mock.Call) {
    if call != nil {
        var newList []*mock.Call
        for _, c := range call.Parent.ExpectedCalls {
            if c.Method != call.Method {
                newList = append(newList, c)
            }
        }
        call.Parent.ExpectedCalls = newList
    }
}

// caller side
unsetFn(mockInstance.On("SOME_METHOD_YOU_WANT_TO_UNSET"))
lisitsky commented 1 year ago

Hello @gordonlee . I have a fix #1250 but have no permission merge it. Could you help promote it?

hugoboos commented 1 year ago

Running in the same problem. A fix would be appreciated 😄