Submit Search
Upload
Testing with mock object
•
4 likes
•
870 views
wear
Follow
shanghaionrails first event presentation by zhanyuanyi
Read less
Read more
Technology
Report
Share
Report
Share
1 of 41
Download now
Download to read offline
Recommended
アプリケーションを作るときに考える25のこと
アプリケーションを作るときに考える25のこと
Takafumi ONAKA
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
railsconf
RSpec. Part 1
RSpec. Part 1
Vladimir Dementyev
Eu odeio OpenSocial
Eu odeio OpenSocial
Carlos Brando
Web Components: What, Why, How, and When
Web Components: What, Why, How, and When
Peter Gasston
Let's write secure drupal code!
Let's write secure drupal code!
Balázs Tatár
From University to Real Life
From University to Real Life
Jose Galarza
Acceptance Testing with Webrat
Acceptance Testing with Webrat
Luismi Cavallé
Recommended
アプリケーションを作るときに考える25のこと
アプリケーションを作るときに考える25のこと
Takafumi ONAKA
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
railsconf
RSpec. Part 1
RSpec. Part 1
Vladimir Dementyev
Eu odeio OpenSocial
Eu odeio OpenSocial
Carlos Brando
Web Components: What, Why, How, and When
Web Components: What, Why, How, and When
Peter Gasston
Let's write secure drupal code!
Let's write secure drupal code!
Balázs Tatár
From University to Real Life
From University to Real Life
Jose Galarza
Acceptance Testing with Webrat
Acceptance Testing with Webrat
Luismi Cavallé
Delete statement in PHP
Delete statement in PHP
Vineet Kumar Saini
Meta Programming with JavaScript
Meta Programming with JavaScript
jeresig
Embracing Capybara
Embracing Capybara
Tim Moore
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Rafael Specht da Silva
Select * from internet
Select * from internet
markandey
JBoss Fuse - Fuse workshop Error Handling
JBoss Fuse - Fuse workshop Error Handling
Christina Lin
Opensocial Codelab
Opensocial Codelab
Pieter De Schepper
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Johannes Hoppe
Ajax On S2 Odp
Ajax On S2 Odp
ghessler
[PyConZA 2017] Web Scraping: Unleash your Internet Viking
[PyConZA 2017] Web Scraping: Unleash your Internet Viking
Andrew Collier
Java script events
Java script events
AbhishekMondal42
JavaScript Operators
JavaScript Operators
Dr. Jasmine Beulah Gnanadurai
Functional testing with capybara
Functional testing with capybara
koffeinfrei
10. CodeIgniter vederea inregistrarilor
10. CodeIgniter vederea inregistrarilor
Razvan Raducanu, PhD
Programming JNI
Programming JNI
Kunio Murasawa
Growing jQuery
Growing jQuery
gueste8d8bc
Analisis Forense Memoria RAM
Analisis Forense Memoria RAM
Conferencias FIST
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
Jason Morrison
Pemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan Session
Nur Fadli Utomo
My Family
My Family
juanbubuyog
Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese Version
Libin Pan
Chinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 Zh
Jesse Cai
More Related Content
What's hot
Delete statement in PHP
Delete statement in PHP
Vineet Kumar Saini
Meta Programming with JavaScript
Meta Programming with JavaScript
jeresig
Embracing Capybara
Embracing Capybara
Tim Moore
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Rafael Specht da Silva
Select * from internet
Select * from internet
markandey
JBoss Fuse - Fuse workshop Error Handling
JBoss Fuse - Fuse workshop Error Handling
Christina Lin
Opensocial Codelab
Opensocial Codelab
Pieter De Schepper
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Johannes Hoppe
Ajax On S2 Odp
Ajax On S2 Odp
ghessler
[PyConZA 2017] Web Scraping: Unleash your Internet Viking
[PyConZA 2017] Web Scraping: Unleash your Internet Viking
Andrew Collier
Java script events
Java script events
AbhishekMondal42
JavaScript Operators
JavaScript Operators
Dr. Jasmine Beulah Gnanadurai
Functional testing with capybara
Functional testing with capybara
koffeinfrei
10. CodeIgniter vederea inregistrarilor
10. CodeIgniter vederea inregistrarilor
Razvan Raducanu, PhD
Programming JNI
Programming JNI
Kunio Murasawa
Growing jQuery
Growing jQuery
gueste8d8bc
Analisis Forense Memoria RAM
Analisis Forense Memoria RAM
Conferencias FIST
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
Jason Morrison
Pemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan Session
Nur Fadli Utomo
My Family
My Family
juanbubuyog
What's hot
(20)
Delete statement in PHP
Delete statement in PHP
Meta Programming with JavaScript
Meta Programming with JavaScript
Embracing Capybara
Embracing Capybara
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Não alimente os trolls: JavaScript é bonito - FrontInSM 2015
Select * from internet
Select * from internet
JBoss Fuse - Fuse workshop Error Handling
JBoss Fuse - Fuse workshop Error Handling
Opensocial Codelab
Opensocial Codelab
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Ajax On S2 Odp
Ajax On S2 Odp
[PyConZA 2017] Web Scraping: Unleash your Internet Viking
[PyConZA 2017] Web Scraping: Unleash your Internet Viking
Java script events
Java script events
JavaScript Operators
JavaScript Operators
Functional testing with capybara
Functional testing with capybara
10. CodeIgniter vederea inregistrarilor
10. CodeIgniter vederea inregistrarilor
Programming JNI
Programming JNI
Growing jQuery
Growing jQuery
Analisis Forense Memoria RAM
Analisis Forense Memoria RAM
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
Pemrograman Web 9 - Input Form DB dan Session
Pemrograman Web 9 - Input Form DB dan Session
My Family
My Family
Similar to Testing with mock object
Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese Version
Libin Pan
Chinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 Zh
Jesse Cai
T1
T1
TH Schee
Ajax и будущее Java Script
Ajax и будущее Java Script
Constantin Kichinsky
4. Метапрограмиране
4. Метапрограмиране
Stefan Kanev
php part 2
php part 2
Shagufta shaheen
Ruby on Rails Tutorial Part I
Ruby on Rails Tutorial Part I
Wei Jen Lu
技術トレンディセミナー フレームワークとしてのTrac
技術トレンディセミナー フレームワークとしてのTrac
terada
Theme Development and Customization
Theme Development and Customization
Aniket Pant
DOSUG Intro to JQuery JavaScript Framework
DOSUG Intro to JQuery JavaScript Framework
Matthew McCullough
Reloaded
Reloaded
Shunsaku Kudo
事件模型探究
事件模型探究
ematrix
Why Our Code Smells
Why Our Code Smells
TiNguyn863920
Routing System In Symfony 1.2
Routing System In Symfony 1.2
Alex Demchenko
Jslunch6
Jslunch6
Nao Haida
Spring Framework勉強会
Spring Framework勉強会
Masakazu Matsushita
Sphinx on Rails
Sphinx on Rails
freelancing_god
HCI: Design Process
HCI: Design Process
Charles (XXC) Chen
Ontology-based Content Management System (ICIM 2008)
Ontology-based Content Management System (ICIM 2008)
Brian Hsu
Why Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary Thing
Chris Reynolds
Similar to Testing with mock object
(20)
Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese Version
Chinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 Zh
T1
T1
Ajax и будущее Java Script
Ajax и будущее Java Script
4. Метапрограмиране
4. Метапрограмиране
php part 2
php part 2
Ruby on Rails Tutorial Part I
Ruby on Rails Tutorial Part I
技術トレンディセミナー フレームワークとしてのTrac
技術トレンディセミナー フレームワークとしてのTrac
Theme Development and Customization
Theme Development and Customization
DOSUG Intro to JQuery JavaScript Framework
DOSUG Intro to JQuery JavaScript Framework
Reloaded
Reloaded
事件模型探究
事件模型探究
Why Our Code Smells
Why Our Code Smells
Routing System In Symfony 1.2
Routing System In Symfony 1.2
Jslunch6
Jslunch6
Spring Framework勉強会
Spring Framework勉強会
Sphinx on Rails
Sphinx on Rails
HCI: Design Process
HCI: Design Process
Ontology-based Content Management System (ICIM 2008)
Ontology-based Content Management System (ICIM 2008)
Why Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary Thing
Recently uploaded
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Safe Software
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Martijn de Jong
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Delhi Call girls
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Katpro Technologies
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Drew Madelung
Slack Application Development 101 Slides
Slack Application Development 101 Slides
praypatel2
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
apidays
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
Sinan KOZAK
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Michael W. Hawkins
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
Pooja Nehwal
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
Allon Mureinik
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
ThousandEyes
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
Paola De la Torre
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
The Digital Insurer
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
The Digital Insurer
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
The Digital Insurer
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
Recently uploaded
(20)
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Slack Application Development 101 Slides
Slack Application Development 101 Slides
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Testing with mock object
1.
使 用 Mock
object 进 行 测 试 张 元 一
2.
什么是Mock object?
3.
wikipedia Mock object 是面向对象编程中 ,对真实对象的行为以可控的 方式进行模拟的一种虚拟对象
4.
汽车碰撞测试中的模型人
5.
为什么需要Mock object?
6.
1.降低代码耦合
7.
一个改动导致大量测试失败
8.
def test_create
post :create, :user => {:name => 'test'} end def test_update put :update, :user => {:name => 'new'} end
9.
add_column :first_name add_column :last_name remove_column
:name 挂了!
10.
2. 人生短暂,珍惜时间
11.
不要重复测试 A测试例中已经测试过的代 码没必要再在B测试例中进 行测试,尤其是这些代码很耗 时
12.
# post_test.rb def test_should_create_post
post = Post.new(...) assert post.valid? end class post validates_presence_of :xxx end
13.
# posts_controller_test.rb def test_should_create_post
post :create, :post => {...} ... end
14.
# posts_controller.rb def create
post = Post.new(params[:post]) if post.save ... end 重复了!
15.
3.让自己更轻松
16.
等待是人世间最痛苦的事情之一, 尤其是你苦苦等待的结果居然是:
Failure!
17.
如何使用Mock object?
18.
Mocha, Flex Mock
or RSpec
19.
Mocha
20.
@post = mock(“post”) @post
= Post.new
21.
@post = mock(“post”) @post.digg #<Mock:post>.digg
- expected calls: 0, actual calls: 1
22.
@post.expects(:digg) @post.instance_eval {
def digg ... end }
23.
def test_xxx
@post.expects(:digg) end #<Mock:post>.digg - expected calls: 1, actual calls: 0
24.
@post.expects(:digg).once at_least(min) at_least_once at_most(max) at_most_once never times(num)
25.
if @post.digg #
nil ... else ... end
26.
@post.expects(:digg)
.returns(true) @post.expects(:digg) .raises(exception)
27.
@post.digg(@blocked) # false @post.digg(@unblocked)
# true @post.expects(:digg) .with(any_of(User.blocked)) .returns(false) @post.expects(:digg) .with(any_of(User.unblocked)) .returns(true)
28.
all_of any_of anything has_entry(key, value) has_key(key) has_value(value) includes(item) instance_of(klass) kind_of(klass) regexp_matches(regexp)
29.
@post.expects(:method)
.at_least(0) @post.expects(:method) .at_least(0) .returns(:result) @post.stubs(:method) @post.stubs(:method => :result)
30.
@post = stub_everything('post'
:method => :result) @post.method1 # nil @post.method2 # nil @post.method # :result
31.
Mocha on Rails
32.
def test_create
post :create, :user => {:name => 'test'} end add_column :first_name add_column :last_name remove_column :name
33.
def test_should_create_user
Post.expects(:new) .returns(@post) @post.expects(:save) .returns(true) post :create, :user => {} assert_redirect_to user_path(@user) end
34.
告别Fixture!
35.
# teachers_students.yml
one: teacher_id: 1 不够直观 student_id: 1 two: teacher_id: 1 浪费时间 student_id: 2 three: teacher_id: 2 student_id: 3
36.
def setup
@user = User.new(:name=>'test') @post1 = Post.new(:title=>'post1') end def test_should_show_post User.expects(:find) .returns(@user) @user.posts.expects(:find) .returns(@post1) get :show, :id=>1, :user_id=>1 assert_response :success end
37.
RSpec
38.
it “should create
a new user” do User.should_receive(:new) .and_return(@user) User.stub!(:save) .and_return(true) post :create, :user => {} response.should redirect_to(user_path(@user)) end
39.
Flex Mock? 同样的思想,不同的实现!
40.
问题?
41.
谢谢!
Download now