Skip to content

Return() and RunAndReturn() shouldn't be allowed to combine #1120

@halmai

Description

@halmai

Description

I had something like this in my code:

p.backend.EXPECT().
	F(mock.Anything).
	RunAndReturn(func(...) error {
		fmt.Println("hello")
		return nil
	}).
	Return(nil).
	Once()

The "hello" string was not printed. If I replaced it with a panic(), it didn't panic either.

Expected behavior

I expected the function in RunAndReturn() to be executed, and therefore, "hello" printed.

It turned out that if both Return() and RunAndReturn() are specified then the RunAndReturn() is not executed at all.

It totally makes sense, if we know what the function needs to return then running something beforehand is useless.

I cannot see why someone would use both functions and I think it is only a possible source of confusion, as it was in my case. For this reason, I suggest that if both Return and RunAndReturn are specified then the test should stop with an error message like this:

Return() and RunAndReturn() cannot be used simultaneously

Mockery version

v2.51.0

Go version

v1.25.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions