Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
unit tests in ruby
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
SiweiShen
January 03, 2013
Technology
1
97
unit tests in ruby
introduced how to write basic unit tests and how to fasten the execution speed.
SiweiShen
January 03, 2013
Tweet
Share
More Decks by SiweiShen
See All by SiweiShen
Ruby introduction to QA people
sg552sg552
1
71
ruby introduction ( Chinese)
sg552sg552
1
70
meta programming ruby
sg552sg552
5
230
Other Decks in Technology
See All in Technology
クレジットカード決済基盤を支えるSRE - 厳格な監査とSRE運用の両立 (SRE Kaigi 2026)
capytan
6
2.6k
Introduction to Bill One Development Engineer
sansan33
PRO
0
360
Kiro IDEのドキュメントを全部読んだので地味だけどちょっと嬉しい機能を紹介する
khmoryz
0
160
toCプロダクトにおけるAI機能開発のしくじりと学び / ai-product-failures-and-learnings
rince
6
5.5k
GCASアップデート(202510-202601)
techniczna
0
250
オープンウェイトのLLMリランカーを契約書で評価する / searchtechjp
sansan_randd
3
650
SREのプラクティスを用いた3領域同時 マネジメントへの挑戦 〜SRE・情シス・セキュリティを統合した チーム運営術〜
coconala_engineer
2
600
GSIが複数キー対応したことで、俺達はいったい何が嬉しいのか?
smt7174
3
140
Data Hubグループ 紹介資料
sansan33
PRO
0
2.7k
AzureでのIaC - Bicep? Terraform? それ早く言ってよ会議
torumakabe
1
350
ClickHouseはどのように大規模データを活用したAIエージェントを全社展開しているのか
mikimatsumoto
0
190
CDKで始めるTypeScript開発のススメ
tsukuboshi
1
320
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
97
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
110
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
280
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
RailsConf 2023
tenderlove
30
1.3k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
72
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
60
42k
How STYLIGHT went responsive
nonsquared
100
6k
Transcript
Mocks in unit test of Ruby 申思维 Siwei
什 要用 元 ? 为 么 单 测试 Why UT?
➲ 1. 把 手 操作 下来, 成自 。 动 记录 变 动 Manual Operations => Automatic ➲ 2. 可以多次运行,是持 集成 的基 。 续 测试 础 The Basic of Continous Integration ➲ 3. 运行方便,极大减 程序 担。 轻 员负 Save developer's life.
比 对 IRB: > a = 1 > b =
2 > puts a + b > 3 Unit test: def test_should_show_1_add_2 a = 1; b = 2; assert a+b == 3 end
中的 现实 问题 Problems we met 某个 目, 始了一年, 运行需要与
项 开 3 个外部系 配合: 统 An existing app interacting with 3 external systems: ➲ 1. web service system ➲ 2. authentication system ➲ 3. authorization system
中的 现实 问题 The situation of test cases. 元 的情况:
单 测试 ➲ 1. 400 test cases. 2. too slow. 20+ minutes. 3. not stable , 5% 的 WEB SERVICE test cases will fail.
后果 Results ➲ 人 越来越不想运行 元 。 开发 员 单
测试 Developers don't want to use Unit tests. ➲ 程中,容易走神。( 开发过 20 分 的 ) 钟 时间 Developers often lost their focus during the coding. ➲ 出 率太高。 错 Not stable 直接 致了,系 以 ,想想就 大。 导 统难 维护 头 用 成 人 ,体 十分不好。 户 为测试 员 验
解决方案 Solutions 目 : 短 元 运行的 。 标 缩
单 测试 时间 Target :shorten the running time. 手段: 使用 模 象 和方法。 拟对 ➲ 原来: 用一次,需要 调 15 秒。 Before : 15 s ➲ 模 象: 拟对 0.00xx 秒。 Now: 0.000x s 1. Mock : for objects. 2. Stub : for methods.
Mock Object Real object: ( cost 15 seconds) class DeviceResource
def query(parameters) return json.decode(service(parameters)) end end Mock object: ( 0.000x seconds) class DeviceResource def query(parameters) return “{ status: success, id: 3 }” end end
Stub a method def get_name return “Jim” end get_name #
=> “Jim” require 'rspec-mock' stub(:get_name).and_return (“Kate”) get_name # => “Kate”
Two Mode for Running the Test ➲ 1. the mock
mode: $ rpsec spec ➲ 2. also supports the real mode: $ rspec spec with_real_webservice=true
The result ➲ 目更好了 项 Project becomes better: 1. 代
量更高 码质 .higher code quality. 2. 速度更快 开发 faster development & delivery. 3. 更少。用 更 意 错误 户 满 . less bugs.
结论 Conclusion ➲ 是使用 元 。 总 单 测试 Always
use unit tests. ➲ 元 :可重 ,速度快, 定。( 让单 测试 复 稳 make unit tests faster, stable and reusable )
Thank you ! 谢谢 申思 维 Siwei Shen
[email protected]
http://shensiwei.com