Skip to content

mock: add OnF method#1912

Open
burik666 wants to merge 1 commit into
stretchr:masterfrom
burik666:OnFn
Open

mock: add OnF method#1912
burik666 wants to merge 1 commit into
stretchr:masterfrom
burik666:OnFn

Conversation

@burik666

Copy link
Copy Markdown

Summary

Adds method OnFn to mock.Mock and mock.Call to allow defining mock expectations using function references instead of string method names.

Changes

  • Added OnFn method to mock.Mock and mock.Call.
  • OnFn extracts the function name using reflection and runtime information (runtime.FuncForPC).
  • Provides a more type-safe, refactor-friendly alternative to On.

Motivation

Currently, mock expectations are defined using string literals for method names, which can be error-prone and break easily during refactoring.
OnFn allows developers to use actual function references, improving type safety and maintainability.

Related issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use function pointers to set up mock expectations

1 participant