嘲笑する
Goモックとは何ですか
Go モックは、開発者がインターフェースのモック構造体を作成し、その動作を定義できるようにするモック フレームワークです。
たとえば、Reponsitory のモック実装を生成し、特定の入力に基づいて期待される出力を定義するのに役立ちます。
元のリポジトリ: https://github.com/golang/mock
現在はメンテナンスされていないため、代わりに https://github.com/uber-go/mock を使用する必要があります。
インストール
go install go.uber.org/mock/mockgen@latest
主な特徴
- モック生成: gomock には、インターフェイスのモック実装を自動的に生成する、mockgen と呼ばれるツールが含まれています。
-
柔軟な期待: gomock を使用すると、次のようなモック オブジェクトの動作に対する正確な期待を定義できます。
- 受け取るべき引数
- そして返される値
- メソッドを呼び出す特定の回数、最小または最大回数
モッゲンコマンド
このドキュメントに従って、mockgen CLI の使用方法を理解してください:
https://github.com/uber-go/mock?tab=readme-ov-file#running-mockgen
使用法
いくつかのメソッドを備えた IUserRepo インターフェイスがあるとします。
ports.go
package user type IUserRepo interface { GetUserByID(id int) (*User, error) Insert(user User) error Update(id int, user User) error }
domain.go
package user type User struct { ID int Name string }
service.go
package user import "fmt" type UserService struct { repo IUserRepo } var invalidUserIDError = fmt.Errorf("invalid user id") func (u *UserService) Upsert(user User) error { if user.ID <= 0 { return invalidUserIDError } existingUser, err := u.repo.GetUserByID(user.ID) if err != nil { return err } if existingUser == nil { return u.repo.Insert(user) } return u.repo.Update(user.ID, user) }
1.モックジェンを実行してモックインスタンスを生成します
go run go.uber.org/mock/mockgen@latest -source=interface.go -destination=mock.go -package=user
- source: リポジトリ インターフェースを含むファイルを指定します。
- destination: 生成されたモック コードが書き込まれるファイルを指定します。
- package: 生成されたモックのパッケージ名を指定します。
2.期待値を指定
service_test.go
package user import ( "go.uber.org/mock/gomock" "github.com/stretchr/testify/assert" "testing" ) func TestUpsertUser(t *testing.T) { mockCtl := gomock.NewController(t) defer mockCtl.Finish() tests := []struct { name string user User specifyFunctionCalls func(mock *MockIUserRepo) expectedError error }{ { user: User{ID: 1, Name: "User 1"}, name: "Should insert", specifyFunctionCalls: func(mockRepo *MockIUserRepo) { mockRepo.EXPECT().GetUserByID(1).Return(nil, nil).Times(1) mockRepo.EXPECT().Insert(User{ID: 1, Name: "User 1"}).Return(nil).Times(1) }, }, { name: "User existed - Should update", user: User{ID: 1, Name: "New User Name"}, specifyFunctionCalls: func(mockRepo *MockIUserRepo) { mockRepo.EXPECT().GetUserByID(1).Return(&User{ID: 1, Name: "User 1"}, nil).Times(1) mockRepo.EXPECT().Update(1, User{ID: 1, Name: "New User Name"}).Return(nil).Times(1) }, }, { expectedError: invalidUserIDError, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { mockRepo := NewMockIUserRepo(mockCtl) if test.specifyFunctionCalls != nil { test.specifyFunctionCalls(mockRepo) } userService := UserService{repo: mockRepo} err := userService.Upsert(test.user) assert.Equal(t, test.expectedError, err) }) } }
上記のテスト ファイル内:
- specifyFunctionCalls を使用すると、どの関数を呼び出す必要があるか、およびこれらの関数を何回呼び出す必要があるかなど、モック関数の期待値をカスタマイズできます。
- specifyFunctionCalls 仕様に冗長な関数を追加しようとすると (例: 最初のテストに mockRepo.EXPECT().Update(....) を追加する)、テストは次のエラーにより失敗します: missing call(s) .
- サービスが指定されていない関数を呼び出す場合、テストはエラーにより失敗します: そのレシーバーに対してメソッド "Insert" の呼び出しが予期されていません。
ソースコード
https://github.com/huantt/gomock-demo
以上が嘲笑するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Golangは、パフォーマンスとスケーラビリティの点でPythonよりも優れています。 1)Golangのコンピレーションタイプの特性と効率的な並行性モデルにより、高い並行性シナリオでうまく機能します。 2)Pythonは解釈された言語として、ゆっくりと実行されますが、Cythonなどのツールを介してパフォーマンスを最適化できます。

Golangは並行性がCよりも優れていますが、Cは生の速度ではGolangよりも優れています。 1)Golangは、GoroutineとChannelを通じて効率的な並行性を達成します。これは、多数の同時タスクの処理に適しています。 2)Cコンパイラの最適化と標準ライブラリを介して、極端な最適化を必要とするアプリケーションに適したハードウェアに近い高性能を提供します。

goisidealforforbeginnersandsutable forcloudnetworkservicesduetoitssimplicity、andconcurrencyfeatures.1)installgofromtheofficialwebsiteandverify with'goversion'.2)

Golangは迅速な発展と同時シナリオに適しており、Cは極端なパフォーマンスと低レベルの制御が必要なシナリオに適しています。 1)Golangは、ごみ収集と並行機関のメカニズムを通じてパフォーマンスを向上させ、高配列Webサービス開発に適しています。 2)Cは、手動のメモリ管理とコンパイラの最適化を通じて究極のパフォーマンスを実現し、埋め込みシステム開発に適しています。

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

GolangとCにはそれぞれパフォーマンス競争において独自の利点があります。1)Golangは、高い並行性と迅速な発展に適しており、2)Cはより高いパフォーマンスと微細な制御を提供します。選択は、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。

GolangisidealforBuildingsCalables Systemsduetoitsefficiency andConcurrency、Whilepythonexcelsinquickscriptinganddataanalysisduetoitssimplicityand vastecosystem.golang'ssignencouragesclean、readisinediteNeditinesinedinediseNabletinedinedinedisedisedioncourase
