Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Mocking in Rust Applications
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
taiki45
September 10, 2024
Technology
840
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Mocking in Rust Applications
Talk at "RustのWebアプリ開発 LT道場 〜テスト編〜"
https://findy.connpass.com/event/327824/
taiki45
September 10, 2024
More Decks by taiki45
See All by taiki45
認知負荷を吸収し、プロダクトをまたぐPR Preview基盤の設計事例
taiki45
2
710
Error Handling in Rust Applications
taiki45
3
940
Efficient Platform for Security and Compliance
taiki45
6
1.7k
RustでAWS Lambda functionをいい感じに書く
taiki45
2
940
SPIFFE Meetup Tokyo #2 LT: Envoy SDS
taiki45
0
890
builderscon Tokyo 2019: Intro Service Mesh
taiki45
6
3.9k
NoOps Meetup Tokyo #7: 入門サービスメッシュ
taiki45
4
2k
CloudNative Days Tokyo 2019: Understanding Envoy
taiki45
3
3.7k
Cloud Native Meetup Tokyo #8 ServiceMesh Day Recap
taiki45
2
450
Other Decks in Technology
See All in Technology
NDIでApple Vision Proにリアルタイム映像送信
jyouryuusui
0
140
JAWS-UGグッズ職人仕事の流儀
kentapapa
0
130
S3 Filesで挑む! 八戸せんべい汁の食べ頃問題
usanchuu
0
130
PagerDuty Tech Hub-鳴らすけれど、人は動かない
ezaki
0
190
事業成長とデータ活用を止めないためのマネジメントへ挑戦
hiracky16
0
420
コンテナ運用におけるネットワーク・コンテナコストの削減方法/How to Reduce Network and Container Costs in Container Operations
masakiokuda
2
330
Getting Started with Google Antigravity
glaforge
0
340
CloudWatch Logsのコスト最適化:どこまでAIに任せてどこから人間が承認すべきか
yama3133
0
120
CTO Night|NotionのAIエージェントとFDE
kzkhykw1991
0
280
大型犬好きによるPugの紹介
yamanoku
1
160
サブエージェントと Claude Mods で作る独自 Advisor ツールの話
kenichiodo
2
180
Gemini Intelligence 入門 ― AppFunctions と Gemini Nano
takuroabe
0
330
Featured
See All Featured
It's Worth the Effort
3n
188
29k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Technical Leadership for Architectural Decision Making
baasie
3
600
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Balancing Empowerment & Direction
lara
6
1.3k
Chasing Engaging Ingredients in Design
codingconduct
0
370
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
So, you think you're a good person
axbom
PRO
2
2.2k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
Leo the Paperboy
mayatellez
10
2.4k
Transcript
© 2024 Finatext Holdings Ltd. Mocking in Rust Applications Taiki
Ono, Finatext
© 2024 Finatext Holdings Ltd. Taiki Ono @taiki45 1
None
© 2024 Finatext Holdings Ltd. 1. モックするまでの道のり 2. モックを定義する・使う 例として使ったコードは
https://github.com/Finatext/orgu に全貌あります 今回の話は単体テストくらいの粒度のテストをスコープにしています 目次 3
© 2024 Finatext Holdings Ltd. モックするまでの道のり 4
© 2024 Finatext Holdings Ltd. テスト対象のロジックは外部依存について差し替え可能であることが望ましいです(後述) Rustでは交換可能性はGenericsまたはTrait Objectsを使って実現できます • Generics
(+ Trait Bounds): コンパイル時に具体的な型が分かるケースで利用します。コンパイラがそれぞ れの具体的な型用のコードを生成する。静的ディスパッチ • Trait Objects: コンパイル時に具体的な型が分からないケースで利用します。実行時のメソッド呼び出し時 に仮想関数テーブルを参照して実際の呼び出し関数(メソッド)を決定します。動的ディスパッチ 交換可能性を導入する 5
© 2024 Finatext Holdings Ltd. 非同期ジョブのロジックという例: 「GitHub Checks APIを使ってCheckRunを作成して、Git cloneでリポジトリをクローンして、指定された外部
コマンドを実行する」 テスト時にモックしたい部分 • GitHub APIを呼ぶ: GithubClient trait • Git clone: Checkout trait →このケースでは例えば、Handlerという構造体を定義してそれぞれの外部依存を持たせる 交換可能性の例 6
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: GithubClient trait 7
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: Checkout trait 8
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: Handler 9
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: Handlerの初期化関数 10
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: ドメインロジック 11
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: 初期化 12
© 2024 Finatext Holdings Ltd. 静的ディスパッチのアプローチ: テスト時の初期化 13
© 2024 Finatext Holdings Ltd. 動的ディスパッチのアプローチ: Handler定義 14 ※動的ディスパッチ
© 2024 Finatext Holdings Ltd. 動的ディスパッチのアプローチ: 初期化 15
© 2024 Finatext Holdings Ltd. ケースバイケース Webアプリケーションの場合はIOの方がはるかにでかいので、生産性その他で決めるといいです 静的ディスパッチ vs 動的ディスパッチ
16
© 2024 Finatext Holdings Ltd. axum crateのStateのような仕組みを使う http crateのRequest構造体にはexstensionの仕組みがあるので、このexstensionに状態を毎回セットする tower::Serviceを作ると同じようなことが実現できる(はず)
ある程度の規模以上だと依存管理が煩雑になりそうなのでDependency Injdectionライブラリが有効そうだが少な い。DIパターンもデファクトがあるわけではなさそうなので今後の発展に期待(っぽい) Webアプリケーションでの交換可能性の実現 17
© 2024 Finatext Holdings Ltd. Webアプリケーションでの交換可能性の実現 18
© 2024 Finatext Holdings Ltd. モックを定義する・使う 19
© 2024 Finatext Holdings Ltd. • #[automock] ◦ 先の例のような典型的なケースではこのattribute macroで自動生成する
• mock! ◦ #[automock]マクロではダメなケースでこのproc macroで半手動(?)で生成する 名前に "Mock" prefixが付いた構造体が定義されるので、::newか::defaultで構造体を初期化する モック構造体の専用のメソッドを使ってモック対象のtraitのメソッド呼び出しのexpectationを設定する Expectation設定用のメソッド名は "expect_<original_method_name>" という規約 デストラクタで設定されたexpectationを検証する mockall crate 20
© 2024 Finatext Holdings Ltd. 例: モック定義 21
© 2024 Finatext Holdings Ltd. 例: モックを使う 22
© 2024 Finatext Holdings Ltd. 例: メソッドの引数の検証 23
© 2024 Finatext Holdings Ltd. • 構造体の実装のモックはできるが名前空間上でハック(?)が必要でやや微妙 ◦ 後述 •
Associated functions(static methods?)のモックはexpectationの設定がグローバルになるので、テスト の並列実行で同期ロジックが必要 ◦ なるべく避けたい • モジュール上の関数もモック可能だが名前空間上のハックが必要、かつexpectationの設定がグローバルにな る ◦ なるべく避けたい • モック対象のtraitのメソッドが参照を返す場合は制限あり • モック対象のtraitのメソッドの返り値がimpl Traitの場合は、mockall内部でBox<dyn Trait>に変換するの で制限あり • モック対象のtraitのメソッドの返り値がimpl Futureの場合は、返り値をpinningする必要あり ◦ 実際はasyncキーワードとasync-trait crateを使うことが多い(?) ◦ 後述 ドキュメントとexamplesがかなり丁寧でその他のケースも網羅してるので困った時はどうぞ mockallの注意点 24
© 2024 Finatext Holdings Ltd. async_traitマクロの手前にautomockマクロを置く。言い換えると、async_traitマクロで書き換えられた後の コードに対してautomockマクロでモック生成する async_traitマクロがその名前でimportされている必要あり。内部で使っているっぽい async-trait crateとの併用
25
© 2024 Finatext Holdings Ltd. 構造体の実装のモック 26
© 2024 Finatext Holdings Ltd. Associated Functionsの交換 27
© 2024 Finatext Holdings Ltd. 実用Rustアプリケーション開発 28 https://zenn.dev/taiki45/books/pragmatic-rust-application-development
None