Skip to content

Feature/dynamic return values #1726

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

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

Conversation

jlou2u
Copy link

@jlou2u jlou2u commented Apr 5, 2025

Summary

Adds RunWithReturn which is essentially Run but adds Arguments as the return type and propagates them.

Used this as a starting point #742

Changes

  • Adds RunWithReturn

Motivation

Helps write simple tests that can dynamically calculate return values.

Example (from test)

counter := 0
mockedService.On("TheExampleMethod", Anything, Anything, Anything).
	RunWithReturn(func(args Arguments) Arguments {
		counter++
		a, b, c := args[0].(int), args[1].(int), args[2].(int)
		assert.IsType(t, 1, a)
		assert.IsType(t, 1, b)
		assert.IsType(t, 1, c)
		return Arguments{counter}
	}).
	Twice()

answer, _ := mockedService.TheExampleMethod(2, 4, 5)
assert.Equal(t, 1, answer)

answer, _ = mockedService.TheExampleMethod(44, 4, 5)
assert.Equal(t, 2, answer)

Related issues

#742

gburt and others added 12 commits April 5, 2025 08:29
The comments for the require package were just copied over
from the assert package when generating the functions.
This could lead to confusion because
1. The code-examples were showing examples using the
assert package instead of the require package
2. The function-documentation was not mentioning that
the functions were calling `t.FailNow()` which is some
critical information when using this package.
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.

5 participants