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

feature: export ArgsForCall structs to enable comparing the args as a struct #106

Open
dnephin opened this issue Dec 12, 2018 · 1 comment

Comments

@dnephin
Copy link
Contributor

dnephin commented Dec 12, 2018

I have been using counterfeiter with gotest.tools/assert which uses go-cmp to compare structs. I often find myself using fake.FooArgsForCall(i) and writing an assertion for each of the args.

I would really like to write something like:

expected := myfake.FooArgsForCall{...}
assert.DeepEqual(t, myfake.FooArgsForCall(i), expected)

To make this happen I believe the "argsForCall" struct would need to be exported (currently it is an inline []struct{...}), and we'd need to come up with some new name for FooArgsForCall that returns the struct instead of separate args. I assume the existing FooArgsForCall will need to remain for backwards compatibility.

If there is interest in this change I would be happy to work on it.

@dnephin
Copy link
Contributor Author

dnephin commented Dec 26, 2018

Looking into this I see that Fake.Invocations() can kind of be used for this purpose. It requires using the function name to index into the map, and use untyped []interface{} for the arg list. Exporting the type would remove the need to write a bunch of wrappers around this interface to make the tests readable. Something like:

func expectedCalls(funcCalls ...fakeCall) map[string][][]interface{} {
	expected := make(map[string][][]interface{}, len(funcCalls))
	for _, fake := range funcCalls {
		expected[fake.name] = append(expected[fake.name], fake.args)
	}
	return expected
}

type fakeCall struct {
	name string
	args []interface{}
}

func call(name string, args ...interface{}) fakeCall {
	return fakeCall{name: name, args: args}
}

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

No branches or pull requests

1 participant