SlideShare a Scribd company logo
Test Doubles

Li-Wei Cheng
Outline
•   What're test doubles ?
•   Difference between each test double
•   Examples
•   Compare each test double
What're test doubles
•   Test double 測試替身
    o   A generic term for any case where you replace a
        production object.
           Usually used in unit test
Difference between each test
double

Test Double                   Description
  Dummy       最簡單、最原始的測試替身型別。Dummy 沒有實作,最常用於需要參數值但不
              使用它的情況。Null 可視為是 Dummy,但真的 Dummy 是介面或基底類別的
              衍生,且完全不包含實作。

   Stub       Dummy 的上一級,Stub 是介面或基底類別的最低限度實作。會傳回 Void
              的方法通常完全不包含實作,而會傳回值的方法通常會傳回 Hard Code 的值
              。

    Spy       測試 Spy 類似 Stub,但除了提供用戶端可叫用成員的執行個體,Spy 還會
              記錄叫用了哪些成員,好讓單元測試驗證所叫用的成員是否符合預期。

   Fake       Fake 包含更複雜的實作,通常涉及所繼承型別之不同成員之間的互動。雖然不
              是完整的生產實作,但 Fake 與生產實作很相似,儘管它會採取一些捷徑。

   Mock       Mock 是由 Mock 程式庫動態建立 (其他通常是由測試開發人員使用程式碼來
              產生)。測試開發人員永遠看不到實作介面或基底類別的實際程式碼,但是可以
              設定 Mock 以提供傳回值、預期要叫用的特定成員...等等。視其中的設定而
              定,Mock 的行為可能會像 Dummy、Stub 或 Spy。
Example




          Validation depends on DAO




          Validation depends on Hash
Compare each test double


Test Double           Pros               Cons
  Dummy       很容易建立            不太實用

   Stub       容易建立             彈性有限。從單元測試觀察比較難懂。無
                               法驗證是否有正確叫用成員

   Spy        可以驗證是否有正確叫用成員    彈性有限。從單元測試觀察比較難懂

   Fake       提供半成品的實作,適用於許多   較難建立。可能會因為本身過於複雜而需
              不同的案例            要進行自己的單元測試

   Mock       可有效建立測試替身。可以驗證   學習曲線較陡峭
              是否有正確叫用成員。從單元測
              試觀察比較易懂
Configure the test double
Example

             /// pseudo code for CheckAuthentication
             /// input: string id
             /// input: string password
             /// output: true --> if pass the checking
             ///         false --> else fail the checking

              /// get the password's hash result from DAO
              AccountDAO dao = new AccountDao();
              String passwordHashFromDAO =
                                 mDao.getPasswordHash(id);
              mLog.writeLog(passwordHashFromDAO );
              /// get the id's hash result from Hash
              Hash hash = new Hash();
              String hashResult =
                            mHash.getHashResult(password);
              mLog.writeLoghashResult ();
              /// check the result
          return passwordHashFromDAO.equals(hashResult);
Example -- Dummy
•   Dummy Object
    o   Passed around but never actually used. Usually,
        they are just used to fill parameter list.
Example -- Stub
•   Stub
    o   Provide canned answers to calls made during the
        test, usually not responding at all to anything outside
        what's programmed in for the test.
Example -- Fake
•   Fake
    o   Actually have working implementations, but usually
        take some shortcut which makes them not suitable
        for production.
Example -- Spy
•   Spy
    o   Stubs that also record some information based on
        how they were called. One form of this might be an
        email service that records how many messages it
        was sent.
Example -- Mock
•   Mock
    o   pre-programmed with expectations which form a
        specification of the calls they are expected to
        receive.
        They can throw an exception if they receive a call
        they don't expect and are checked during
        verification to ensure they got all the calls they were
        expecting.

More Related Content

What's hot

Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsP3 InfoTech Solutions Pvt. Ltd.
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]ppd1961
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDPaweł Michalik
 
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers ViewpointSource Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers ViewpointTyler Shields
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit TestingMike Lively
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
YAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl CriticYAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl Criticjoshua.mcadams
 
Unit testing for 40 square software
Unit testing for 40 square softwareUnit testing for 40 square software
Unit testing for 40 square softwareRuben Tan
 
Burp plugin development for java n00bs (44 con)
Burp plugin development for java n00bs (44 con)Burp plugin development for java n00bs (44 con)
Burp plugin development for java n00bs (44 con)Marc Wickenden
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Alex Balhatchet
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2ppd1961
 

What's hot (15)

Python Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers ViewpointSource Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
 
Phpunit testing
Phpunit testingPhpunit testing
Phpunit testing
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit Testing
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
YAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl CriticYAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl Critic
 
Unit testing for 40 square software
Unit testing for 40 square softwareUnit testing for 40 square software
Unit testing for 40 square software
 
null Pune meet - Application Security: Code injection
null Pune meet - Application Security: Code injectionnull Pune meet - Application Security: Code injection
null Pune meet - Application Security: Code injection
 
Burp plugin development for java n00bs (44 con)
Burp plugin development for java n00bs (44 con)Burp plugin development for java n00bs (44 con)
Burp plugin development for java n00bs (44 con)
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
 
Code with style
Code with styleCode with style
Code with style
 

Similar to Test doubles

Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!Ortus Solutions, Corp
 
Python mocking intro
Python mocking introPython mocking intro
Python mocking introHans Jones
 
Make Your SW Component Testable
Make Your SW Component TestableMake Your SW Component Testable
Make Your SW Component TestableLi-Wei Cheng
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Codeeddiehaber
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Dror Helper
 
Using Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit TestingUsing Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit TestingMike Clement
 
PHP Unit-Testing With Doubles
PHP Unit-Testing With DoublesPHP Unit-Testing With Doubles
PHP Unit-Testing With DoublesMihail Irintchev
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesTony Nguyen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesHarry Potter
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesJames Wong
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesFraboni Ec
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesHoang Nguyen
 

Similar to Test doubles (20)

Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
 
Mocking
MockingMocking
Mocking
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
Python mocking intro
Python mocking introPython mocking intro
Python mocking intro
 
Rc2010 tdd
Rc2010 tddRc2010 tdd
Rc2010 tdd
 
Make Your SW Component Testable
Make Your SW Component TestableMake Your SW Component Testable
Make Your SW Component Testable
 
Nodevember 2015
Nodevember 2015Nodevember 2015
Nodevember 2015
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
 
Getting testy with Perl
Getting testy with PerlGetting testy with Perl
Getting testy with Perl
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
Unit testing
Unit testingUnit testing
Unit testing
 
Using Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit TestingUsing Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit Testing
 
PHP Unit-Testing With Doubles
PHP Unit-Testing With DoublesPHP Unit-Testing With Doubles
PHP Unit-Testing With Doubles
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 

More from Li-Wei Cheng

Introduction to AndroidMock
Introduction to AndroidMockIntroduction to AndroidMock
Introduction to AndroidMockLi-Wei Cheng
 
Unit testing in android
Unit testing in androidUnit testing in android
Unit testing in androidLi-Wei Cheng
 
What's software testing
What's software testingWhat's software testing
What's software testingLi-Wei Cheng
 
OO design principle
OO design principleOO design principle
OO design principleLi-Wei Cheng
 

More from Li-Wei Cheng (6)

Introduction to AndroidMock
Introduction to AndroidMockIntroduction to AndroidMock
Introduction to AndroidMock
 
Unit testing in android
Unit testing in androidUnit testing in android
Unit testing in android
 
JUnit
JUnitJUnit
JUnit
 
What's software testing
What's software testingWhat's software testing
What's software testing
 
OO design principle
OO design principleOO design principle
OO design principle
 
UML knowledge
UML knowledgeUML knowledge
UML knowledge
 

Recently uploaded

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀DianaGray10
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsExpeed Software
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsVlad Stirbu
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualityInflectra
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»QADay
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupCatarinaPereira64715
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform EngineeringJemma Hussein Allen
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 

Recently uploaded (20)

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 

Test doubles

  • 2. Outline • What're test doubles ? • Difference between each test double • Examples • Compare each test double
  • 3. What're test doubles • Test double 測試替身 o A generic term for any case where you replace a production object. Usually used in unit test
  • 4. Difference between each test double Test Double Description Dummy 最簡單、最原始的測試替身型別。Dummy 沒有實作,最常用於需要參數值但不 使用它的情況。Null 可視為是 Dummy,但真的 Dummy 是介面或基底類別的 衍生,且完全不包含實作。 Stub Dummy 的上一級,Stub 是介面或基底類別的最低限度實作。會傳回 Void 的方法通常完全不包含實作,而會傳回值的方法通常會傳回 Hard Code 的值 。 Spy 測試 Spy 類似 Stub,但除了提供用戶端可叫用成員的執行個體,Spy 還會 記錄叫用了哪些成員,好讓單元測試驗證所叫用的成員是否符合預期。 Fake Fake 包含更複雜的實作,通常涉及所繼承型別之不同成員之間的互動。雖然不 是完整的生產實作,但 Fake 與生產實作很相似,儘管它會採取一些捷徑。 Mock Mock 是由 Mock 程式庫動態建立 (其他通常是由測試開發人員使用程式碼來 產生)。測試開發人員永遠看不到實作介面或基底類別的實際程式碼,但是可以 設定 Mock 以提供傳回值、預期要叫用的特定成員...等等。視其中的設定而 定,Mock 的行為可能會像 Dummy、Stub 或 Spy。
  • 5. Example Validation depends on DAO Validation depends on Hash
  • 6. Compare each test double Test Double Pros Cons Dummy 很容易建立 不太實用 Stub 容易建立 彈性有限。從單元測試觀察比較難懂。無 法驗證是否有正確叫用成員 Spy 可以驗證是否有正確叫用成員 彈性有限。從單元測試觀察比較難懂 Fake 提供半成品的實作,適用於許多 較難建立。可能會因為本身過於複雜而需 不同的案例 要進行自己的單元測試 Mock 可有效建立測試替身。可以驗證 學習曲線較陡峭 是否有正確叫用成員。從單元測 試觀察比較易懂
  • 8. Example /// pseudo code for CheckAuthentication /// input: string id /// input: string password /// output: true --> if pass the checking /// false --> else fail the checking /// get the password's hash result from DAO AccountDAO dao = new AccountDao(); String passwordHashFromDAO = mDao.getPasswordHash(id); mLog.writeLog(passwordHashFromDAO ); /// get the id's hash result from Hash Hash hash = new Hash(); String hashResult = mHash.getHashResult(password); mLog.writeLoghashResult (); /// check the result return passwordHashFromDAO.equals(hashResult);
  • 9. Example -- Dummy • Dummy Object o Passed around but never actually used. Usually, they are just used to fill parameter list.
  • 10. Example -- Stub • Stub o Provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
  • 11. Example -- Fake • Fake o Actually have working implementations, but usually take some shortcut which makes them not suitable for production.
  • 12. Example -- Spy • Spy o Stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
  • 13. Example -- Mock • Mock o pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.