stretchr / testify

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

Added support for getting the arguments of mock function call for a specific call count #1558

Open mahigadamsetty opened 4 months ago

mahigadamsetty commented 4 months ago

Summary

The purpose of this change is to enable asserting the arguments of a mock function for a specific call count.

Changes

Updated the Mock struct to hold the map of methodName to a slice of calls instead of just a slice of Calls, this will be used to extract the calls for a given methodName easily

Motivation

For instance, when a method is invoked multiple times within the code, and there's a need to verify the arguments for a particular call count, this enhancement proves beneficial.

actualObject.ProcessData("data1")
actualObject.ProcessData("data2")

with the new API, can assert the argument for a second call like this .

returnArgs := mockedObject.ArgsForCallCount(t, "ProcessData", 1)
if assert.Equal(t, 1, len(returnArgs)) {
     assert.Equal(t, "data2", returnArgs[0])            
}

Related issues

N/a