stretchr/testify

mock: AssertCalled confuses separate method calls in assert message

Open

#1 144 ouverte le 12 janv. 2022

Voir sur GitHub
 (3 commentaires) (4 réactions) (0 assignés)Go (1 704 forks)batch import
help wantedpkg-mock

Métriques du dépôt

Stars
 (25 958 stars)
Métriques de merge PR
 (Merge moyen 6j 15h) (2 PRs mergées en 30 j)

Description

I have the following setup:

	mockHandler.
		On("Apply",
			mock.AnythingOfType("*v1.ReplicaSet"),
			mock.AnythingOfType("*v1.Secret"),
			mock.Anything).
		Return(nil).
	mockHandler.
		On("Cleanup", mock.AnythingOfType("*v1.Secret"), mock.Anything).
		Return(nil)

After exercising my code, I want to assert that Cleanup was called:

mockHandler.AssertCalled(t, "Cleanup", mock.AnythingOfType("*v1.Secret"), mock.Anything)

In the case where my code only has called Apply, and not called Cleanup, I get an error like this:

        	Test:       	TestManager_ApplyFailed
        	Messages:   	Expected "Cleanup" to have been called with:
        	            	[*v1.Secret mock.Anything]
        	            	but actual calls were:
        	            	        [nil &Secret{ObjectMeta:{      0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[one:1] [] []  []},Data:map[string][]byte{},Type:,StringData:map[string]string{},Immutable:nil,} <nil>]

This seems weird, because Cleanup does not take those arguments. The explanation is that the "actual calls" in this list are calls to Apply, which are not relevant when looking for calls of Cleanup. At the very least, the list of calls should mention the name of the method.

Guide contributeur