-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
Mocks cannot be generated from *_test.go files #83
Comments
I'd accept a fix for this if you fancied doing a PR @mastertinner ? |
@mastertinner I did not look into the details, but maybe it is just removing Lines 81 to 83 in 81c463c
Edit: updated the link |
you usually don't mock an entire db interface . you just need better architecture at that point . Ie hide that querying logic in the repository and mock the repository, your repository would be an interface that has the stuff your app uses ie findAllUsers .. things like that . you shouldn't really be mocking code you don't own . that's my two cents |
@sudo-suhas What do you think about this? I feel like |
I also think, The new code, works differently in regards to loading the package information, so the hint from above (#83 (comment)) is no longer relevant. After a quick look at the code, I think the relevant spot is moq/internal/registry/registry.go Lines 124 to 127 in 126389e
where we would need to add Tests: true like this:
pkgs, err := packages.Load(&packages.Config{
Mode: mode,
Dir: srcDir,
Tests: true,
}) |
Yes, I also agree that |
A possible workaround is to use a private interface definition: // dynamo.go
package dynamo
import "github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbiface"
//go:generate moq -out ../mocks/dynamodb.go -pkg mocks . dynamoDBAPI:DynamoDBAPI
type dynamoDBAPI = dynamodbiface.DynamoDBAPI |
@sudo-suhas That is an interesting tweak with the alias. |
Because I was in the area where this issue would be solved, I had a quick look. I think it would require a bit of refactoring and added complexity to make this work: For one the registry would need to keep references to and iterate over several packages to get the type and interface definitions. Currently, the Registry keeps just the loaded package:
https://github.com/matryer/moq/blob/v0.2.1/internal/registry/registry.go#L36 Also, running I currently started working with @cloudlena who started this issue and we are using the workaround suggested above and are quite happy with that solution. TL;DR: I'm not sure if the added flexibility would be worth the effort. |
I would like to mock an external interface provided by the AWS SDK. To do so, I create a local type alias:
However, this increases the API surface of my
dynamo
package by adding another public interface to it which can and should not be used except for mocking. To mitigate that, I would like to move the code above into the respective testing packagedynamo_test
. However, when I do so, moq complains about the interface not being found:The text was updated successfully, but these errors were encountered: