0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

mockery でモックを作成する方法

Last updated at Posted at 2022-11-27

はじめに

対応するときに調査に時間がかかったので備忘録として。

バージョンなど

  • 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```

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?