Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mahigadamsetty
Copy link

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

@dolmen dolmen added pkg-mock Any issues related to Mock enhancement labels Mar 4, 2024
Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I do not think this API should be added because it does not encourage best testing practices. If testing for state then a mock can be prepared with the expected calls and AssertExpectations used.

Can you demonstrate a specific test case which can't be written using the existing API?

It's also quite unclear what a call "count" is in this context.

@@ -282,7 +282,7 @@ type Mock struct {
ExpectedCalls []*Call

// Holds the calls that were made to this mocked object.
Calls []Call
Calls map[string][]Call
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

Comment on lines +732 to +733
fmt.Println("ArgsForCallCount")
fmt.Println(m.Calls)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not print.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants