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
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
LoriGlavin3
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
Neo4j
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
LoriGlavin3
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Pim van der Noll
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
Ingrid Airi González
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
Inflectra
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
LoriGlavin3
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
Pixlogix Infotech
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
fnnc6jmgwh
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
Kaya Weers
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
UiPathCommunity
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
itnewsafrica
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
Bernd Ruecker
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
LoriGlavin3
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
Wes McKinney
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
panagenda
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
panagenda
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Alkin Tezuysal
A Framework for Development in the AI Age
A Framework for Development in the AI Age
Cprime
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc
Recently uploaded
(20)
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
A Framework for Development in the AI Age
A Framework for Development in the AI Age
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
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