はじめに
対応するときに調査に時間がかかったので備忘録として。
バージョンなど
- Golang >= 1.19 ( 多分、 <= 1.18 でも大丈夫 )
- mockery == v2.15.0
方法
セットアップ
mockery を参照
自分のコードのモックを作成する
前提条件
以下のようなファイル構成。
.
└── internal
└── app
├── container
│ └── container.go
├── http
│ ├── handler
│ │ ├── health_handler.go
│ │ ├── health_handler_test.go
│ │ ├── send_mail_hander.go
│ │ └── send_mail_service_test.go
│ ├── request
│ │ └── send_mail.go
│ └── response
│ ├── .gitkeep
│ └── result.go
└── service
├── send_mail_service.go
├── send_mail_service_test.go
└── vendors
└── mail
├── amazon_ses_service.go
├── mail_vendor.go
└── send_grid_service.go
方法
% mockery --dir=internal/app/service/ --output=internal/app/service/mocks --all --case=snake --testonly=true --disable-version-string=true
27 Nov 22 09:49 JST INF Starting mockery dry-run=false version=v2.15.0
27 Nov 22 09:49 JST INF Walking dry-run=false version=v2.15.0
27 Nov 22 09:49 JST INF Generating mock dry-run=false interface=AmazonSESService qualified-name=github.com/ijufumi/email-service/internal/app/service/vendors/mail version=v2.15.0
27 Nov 22 09:49 JST INF Generating mock dry-run=false interface=SendGridService qualified-name=github.com/ijufumi/email-service/internal/app/service/vendors/mail version=v2.15.0
27 Nov 22 09:49 JST INF Generating mock dry-run=false interface=SendMailService qualified-name=github.com/ijufumi/email-service/internal/app/service version=v2.15.0
27 Nov 22 09:49 JST INF Generating mock dry-run=false interface=SendMailVendor qualified-name=github.com/ijufumi/email-service/internal/app/service/vendors/mail version=v2.15.0
外部ライブラリのモックを作成する
前提条件
-
aws/aws-sdk-go-v2/service/sesv2
を使っている
方法
% mockery --srcpkg=github.com/aws/aws-sdk-go-v2/service/sesv2 --output=internal/app/service/vendors/mail/stubs --outpkg=stubs --all --case=snake --testonly=true --disable-version-string=true
27 Nov 22 10:02 JST INF Starting mockery dry-run=false version=v2.15.0
27 Nov 22 10:02 JST INF Walking dry-run=false version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=EndpointResolver qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=EndpointResolverFunc qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=GetDedicatedIpsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=HTTPClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=HTTPSignerV4 qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListConfigurationSetsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListContactListsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListContactsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListCustomVerificationEmailTemplatesAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListDedicatedIpPoolsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListDeliverabilityTestReportsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListDomainDeliverabilityCampaignsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListEmailIdentitiesAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListEmailTemplatesAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListImportJobsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=ListSuppressedDestinationsAPIClient qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0
27 Nov 22 10:02 JST INF Generating mock dry-run=false interface=awsEndpointResolverAdaptor qualified-name=github.com/aws/aws-sdk-go-v2/service/sesv2 version=v2.15.0```