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
Simple Mocking in Rust
Search
Takanori Ishibashi
September 05, 2018
Technology
2k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Simple Mocking in Rust
Takanori Ishibashi
September 05, 2018
Other Decks in Technology
See All in Technology
Kiro Crew の Bot (続く役目を任せる窓口) | 目指せ Kiro マスター 12
inariku
PRO
2
360
re:Invent2026 事前勉強会 歴史と愉しみ方10分LT編
toshi_atsumi
0
180
【Oracle AI SPOTLIGHT ウェビナー】【特別開催:日本語で解説!】Oracle AI World 2026 最新情報フィードバック
oracle4engineer
PRO
1
100
Kiro Crew 入門 (手を離しても進む仕事) | 目指せ Kiro マスター 11
inariku
PRO
4
670
現場を動かすAIエージェント#2_製造業現場でのAI活用から見た、ハマる罠と実践知
kintotechdev
2
210
慶應義塾大学 AI実践05 論文執筆の仕組み化
keio_smilab
PRO
7
3.8k
Shared Signals Framework 仕様解説
oidfj
0
350
顧客合意を取りながら進めるAWSコスト削減の実践
optim
0
160
AX時代の自治体支援リソース再配分と役割分担の再構成:戦略とロードマップ(概要)
sishi2026
0
210
FinOpsを導入してマルチプロダクトのコスト最適化を実現する
kworkdev
PRO
0
140
並列度を下げたら速くなった — go build の重複コンパイルを実測で見る
tomoaki25
2
1.1k
Metadata - Section 3 : Dublin Core Standard · DC
mohab1
0
120
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k
Documentation Writing (for coders)
carmenintech
77
5.6k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
560
Rebuilding a faster, lazier Slack
samanthasiow
85
9.8k
HDC tutorial
michielstock
2
1k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
610
Fireside Chat
paigeccino
43
4.1k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
740
The Invisible Side of Design
smashingmag
301
52k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.7k
Making Projects Easy
brettharned
120
6.9k
Transcript
Simple Mocking in Rust Rust入門者の集い #5 @11Takanori
About Me • @11Takanori • 仕事だと、主にKotlin、たまにClojure、Dart、ごくたまにRust、 Elixirを使ってる
mockを使いたい理由 マイクロサービスA,B,Cがあるとする 1. マイクロサービスA,B,Cを組み合わせたE2Eを書く 2. マイクロサービスA単独のE2Eを書く ← 別のマイクロサービスをmockしたい 3. 2が通るようになるまで単体テストと実装を繰り返す(クリーンアーキテクチャのレイ
ヤごと) ← 別のレイヤをmockしたい 4. マイクロサービスBに対しても同様 5. マイクロサービスCに対しても同様 6. マイクロサービスA,B,Cを組み合わせたE2Eが通る こんな感じで開発していると、mockしたくなる
単体テストのmock 今回はライブラリを使用せず、ユーザーにメールを 送る処理を素朴にmockする
#[derive(Clone)] struct Email { from: String, to: String, msg: String,
} struct User { address: String, } trait EmailSender { fn send_mail(&self, msg: &Email) -> Result<String, String>; //mock対象 } 実装
// テスト対象 fn publish_news(msg: &str, sender: &EmailSender, users: &[User]) ->
u32 { let mut count = 0; let mut mail = Email { from: "
[email protected]
".to_string(), to: "".to_string(), msg: msg.to_string(), }; for user in users { mail.to = user.address.to_string(); match sender.send_mail(&mail) { // send_mail(&mail)をmockしたい Ok(_) => count += 1, Err(msg) => println!("Failed to send mail: {}", msg), } } count } 実装
struct MockEmailSender { // send_mailの引数の格納先 sent_mails: RefCell<Vec<Email>>, // RefCellは不変値を可変借用できる }
impl MockEmailSender { fn new() -> Self { MockEmailSender { sent_mails: RefCell::new(Vec::new()), } } } impl EmailSender for MockEmailSender { fn send_mail(&self, mail: &Email) -> Result<String, String> { // RefCellに格納しないと&mut selfを使うことになり、シグニチャーが一致しなくなる self.sent_mails.borrow_mut().push(mail.clone()); Ok("200 OK".to_string()) } } テスト
#[test] fn send_mail_to_users() { let user1 = User { address:
"
[email protected]
".to_string(), }; let user2 = User { address: "
[email protected]
".to_string(), }; let send = publish_news("hello world", &MockEmailSender::new(), &[user1, user2]); assert_eq!(send, 2) // 意図した戻り値になっていることをチェック } テスト
#[test] fn send_correct_mail() { let user = User { address:
"
[email protected]
".to_string(), }; let sender = MockEmailSender::new(); publish_news("hello world", &sender, &[user]); let mails = sender.sent_mails.borrow(); // Ref<Vec<Email>> assert_eq!(mails.len(), 1); // send_mailを呼んだ回数をチェック assert_eq!(mails[0].from, "
[email protected]
"); // 引数のチェック assert_eq!(mails[0].to, "
[email protected]
"); // 引数のチェック assert_eq!(mails[0].msg, "hello world"); // 引数のチェック } テスト
Rustのmockライブラリ • Mockers (nightly) • Double • Mock_Derive (nightly) •
Galvanic-mock (nightly) • Pseudo • Mock-it • Simulacrum デファクトスタンダード的なものは特になさそう。