-
Notifications
You must be signed in to change notification settings - Fork 64
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
Commands which return slices cannot be mocked #27
Comments
It seems like maybe the redis client itself is broken when handling slices. I realized my new code in redismock was using redis.NewCmd instead of redis.NewIntSliceCmd, and I thought that might be the source of the problem. However, when I corrected it, I went right back to the error about
|
This problem only exists for my patched code in #26, which implements ExpectIntSliceDo() and ExpectStringSliceDo(), and then only because the code being tested attempts to use Cmd.Int64Slice() and Cmd.StringSlice(). Those methods are broken in the redis client itself (redis/go-redis#2007 ) because of the mistaken type check against []interface{}. That code does function correctly so long as the code being tested doesn't attempt to use Cmd.Int64Slice() and Cmd.StringSlice() and the problem with those methods should be corrected in the redis client itself. |
There are plenty of commands mocked which purport to return StringSlice or IntSlice, but there are no tests which exercise them. When I attempted to test code which returns a slice from a command, I always receive an error:
which is not at all surprising when you look at the code here: https://github.com/go-redis/redis/blob/cc09f96b8fcabfa25bff2654209c8b711cc9c561/command.go#L375
It always stores the response val for a command as []interface{} and only creates a strongly typed slice after first selecting for []interface{} and then copying each value into a new, strongly typed slice. A strongly typed slice does not satisfy []interface{}
The mock code, on the other hand, assigns a strongly typed slice directly into the command's return val, which causes the call to Slice() to always throw an exception about the wrong type. It seems that SetVal for ExpectedIntSlice and ExpectedStringSlice should both take []interface{} rather than []int64 and []string respectively, or else it should copy into an []interface{} when copying the slice passed to SetVal.
The text was updated successfully, but these errors were encountered: