matryer / matryer/moq

Mocks cannot be generated from *_test.go files

Open
#83 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.2k
Forks
135
Avg merge
11d 6h
Merged PRs (30d)
1

Description

I would like to mock an external interface provided by the [AWS SDK](https://github.com/aws/aws-sdk-go-v2). To do so, I create a local type alias:

```go
// 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

type DynamoDBAPI = dynamodbiface.DynamoDBAPI
```

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 package `dynamo_test`. However, when I do so, moq complains about the interface not being found:

```go
// dynamo_test.go
package dynamo_test

import "github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbiface"

//go:generate moq -out ../mocks/dynamodb.go -pkg mocks . DynamoDBAPI

type DynamoDBAPI = dynamodbiface.DynamoDBAPI
```

```shell
$ go generate ./...

cannot find interface DynamoDBAPI
moq [flags] destination interface [interface2 [interface3 [...]]]
-out string
output file (default stdout)
-pkg string
package name (default will infer)
dynamo_test.go:5: running "moq": exit status 1
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the reproducer in dynamo_test.go and run go generate ./... to confirm that moq cannot find the aliased interface from the *_test.go package. Trace how moq discovers interfaces for that package; done means the DynamoDBAPI alias is found and ../mocks/dynamodb.go is generated successfully without exposing it from dynamo.go.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, go
Domain
devtools, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.