SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Mock driven development (with .NET)
Test Driven Development with Mocks for .NET Framework
By Punit Ganshani
Twitter -
                                                                   @ganshani
                                                                   @codetails

                                                                   LinkedIn -
                                                                   Connect


                                                                   Blog Sites –
                                                                   www.ganshani.com
                                                                   www.codetails.com
                                                                   Open Source –
                                                                   CodeInject, Workflow Extractor,
                                                                   dI.Hook


Punit Ganshani
Punit Ganshani is a Technology Evangelist practising application design & development on C#, WCF,
WP8, WinRT and Open Source. He is an open-source contributor on CodePlex, has several applications
on Windows Phone Store, author of a book, and 18+ whitepapers in international magazines.
Not in scope…

                AGILE MANIFESTO
                          SCRUM
                        KANBAN
                      TFD vs. TAD
Test Driven Development (in a nutshell)

A methodology that states that

test cases should be driving the design decisions
and development
advocates knowing the expected behaviour of our application (or a
part of it), before we write the code
enforces incremental design and allows us to write just enough code
to pass our test

      Test first Development vs Test after development   Doing TDD well
Mock?

Simulated objects that mimic the behavior of real
objects in controlled ways
                                        -Wikipedia


Mock – to deceive, delude, or disappoint.
                                - English Definition
The terminology

Mocks, Stubs, …

▪ Mocks - Mock objects are used to define expectations i.e: In
  this scenario I expect method A() to be called with such and
  such parameters. Mocks record and verify such expectations..
▪ Stubs - Stubs, on the other hand have a different purpose: they
  do not record or verify expectations, but rather allow us to
  “replace” the behaviour, state of the “fake”object in order to
  utilize a test scenario.
▪ Dummy - objects are passed around but never actually used.
  Usually they are just used to fill parameter lists.


                                              Source: http://martinfowler.com/articles/mocksArentStubs.html
Why mock?

• Actual class does not exist - Multiple development streams focusing
  on different components
• Trips to external services, databases take a lot of time to execute
• Non-existent or un-available dependent environments
• A lot of UI input required in real-code
• Abnormal, Unexpected conditions – outages, natural calamities, etc
• Isolate code under test and narrow the scope
When to mock?

▪ Unit Tests
   ▪ Mock the availability of
       ▪ external components
       ▪ non-existent classes
       ▪ services
       ▪ database calls
   ▪ Isolate the code under test and make it less fragile
▪ Integration Tests
   ▪ Do not mock your dependencies, service calls, database calls, etc
   ▪ Focus on checking the integration of all components
   ▪ Should not have any business-specific code
Types of mocks?

▪ Strict
   ▪ Creates brittle tests
   ▪ Throws an exception if you try to use any method that has not explicitly been
     set up to be used.

▪ Normal, Loose
   ▪ Does not throw an exception if you try to use a method that is not set up, it will
     simply return a default value from the method and keep going.
How to mock – Existing Applications?

        Single Responsibility Principle (SRP) - an object should have only a
1   S   single responsibility

        Open/Closed Principle (OCP) - open for extension, but closed for
    O   modification

        Liskov Substitution Principle (LSP) - derived class should not break
    L   assumptions or behavior of base class

        Interface Segregation Principle (ISP) - many specific interfaces are
2   I   better than one general purpose interface (e.g. role interface)

        Dependency Inversion Principle (DIP) - depend upon abstractions, not
3   D   upon concretions


                                                                      Source: http://prasadhonrao.com/tag/solid/
The steps of mocking



 Arrange               Act      Assert

• Data setup
• Mock setup
                 Execute the   Verify the
                  unit test     output
Arrange : Object & Property Mocks



RhinoMocks                            Moq



              NSubstitute
Arrange : Method Mocks




RhinoMocks                            Moq



                        NSubstitute
Arrange: Event Mocks




RhinoMocks




                       NSubstitute
Beware!

  ▪ Remember what is mocked in which test – structure your unit tests
     appropriately
  ▪ Apply mocks only on Unit Tests, not integration tests
  ▪ Forces IoC container based design in and outside tests
  ▪ Dependencies (interfaces for mocks) should not have constructors
  ▪ GUI’s can’t be mocked, business logic should be


Hybrid approach can be followed – Mock some things that are time consuming,
external to your system, unreliable, not easily reachable and use real instances of
others
Further Read

▪ Understanding Mock and frameworks – Part 1 of N – Understanding
  the need of TDD, Mock and getting your application Unit Test ready
▪ Understanding Mock and frameworks – Part 2 of N – Understanding
  Mock Stages & Frameworks – Rhino Mocks, NSubstitute and Moq
▪ Understanding Mock and frameworks – Part 3 of N - Understanding
  how to Mock Methods calls and their Output – Rhino Mocks,
  NSubstitute and Moq
▪ Understanding Mock and frameworks – Part 4 of N - Understanding
  how to Mock Exceptions and Event Subscriptions – Rhino Mocks,
  NSubstitute and Moq

Weitere ähnliche Inhalte

Was ist angesagt?

Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)Foyzul Karim
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard partsShaun Abram
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit TestingShaun Abram
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBaskar K
 
Good Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality CodeGood Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality CodeFlorin Coros
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test PresentationSayedur Rahman
 

Was ist angesagt? (20)

Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Unit test
Unit testUnit test
Unit test
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
 
Good Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality CodeGood Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality Code
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 

Andere mochten auch

Moq Presentation
Moq PresentationMoq Presentation
Moq PresentationLynxStar
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven developmentStephen Fuqua
 
Javascript TDD by example
Javascript TDD by exampleJavascript TDD by example
Javascript TDD by exampleEric Mignot
 
Java script tutorial by example
Java script tutorial by exampleJava script tutorial by example
Java script tutorial by examplemyzyzy
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mockPranalee Rokde
 
Sequential file programming patterns and performance with .net
Sequential  file programming patterns and performance with .netSequential  file programming patterns and performance with .net
Sequential file programming patterns and performance with .netMichael Pavlovsky
 
Unit Testing and Test Driven Development in Unity3D
Unit Testing and Test Driven Development in Unity3DUnit Testing and Test Driven Development in Unity3D
Unit Testing and Test Driven Development in Unity3DAndrew Fray
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitweili_at_slideshare
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Zohirul Alam Tiemoon
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)David Ehringer
 
F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 

Andere mochten auch (17)

Moq Presentation
Moq PresentationMoq Presentation
Moq Presentation
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Javascript TDD by example
Javascript TDD by exampleJavascript TDD by example
Javascript TDD by example
 
Java script tutorial by example
Java script tutorial by exampleJava script tutorial by example
Java script tutorial by example
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Sequential file programming patterns and performance with .net
Sequential  file programming patterns and performance with .netSequential  file programming patterns and performance with .net
Sequential file programming patterns and performance with .net
 
Unit Testing and Test Driven Development in Unity3D
Unit Testing and Test Driven Development in Unity3DUnit Testing and Test Driven Development in Unity3D
Unit Testing and Test Driven Development in Unity3D
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
Tdd in unity
Tdd in unityTdd in unity
Tdd in unity
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 

Ähnlich wie Mock driven development for .NET

ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedJoão Pedro Martins
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
TechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - Trivadis
TechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - TrivadisTechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - Trivadis
TechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - TrivadisTrivadis
 
Architecting a Large Software Project - Lessons Learned
Architecting a Large Software Project - Lessons LearnedArchitecting a Large Software Project - Lessons Learned
Architecting a Large Software Project - Lessons LearnedJoão Pedro Martins
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO DesignBryan Warner
 
DataEngConf SF16 - Methods for Content Relevance at LinkedIn
DataEngConf SF16 - Methods for Content Relevance at LinkedInDataEngConf SF16 - Methods for Content Relevance at LinkedIn
DataEngConf SF16 - Methods for Content Relevance at LinkedInHakka Labs
 
Open, Secure & Transparent AI Pipelines
Open, Secure & Transparent AI PipelinesOpen, Secure & Transparent AI Pipelines
Open, Secure & Transparent AI PipelinesNick Pentreath
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
Technologies for startup
Technologies for startupTechnologies for startup
Technologies for startupDzung Nguyen
 
Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...
Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...
Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...AgileNetwork
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuPhat VU
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfTobiasGoeschel
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
Latest trends in information technology
Latest trends in information technologyLatest trends in information technology
Latest trends in information technologyEldos Kuriakose
 
10 Ways To Improve Your Code( Neal Ford)
10  Ways To  Improve  Your  Code( Neal  Ford)10  Ways To  Improve  Your  Code( Neal  Ford)
10 Ways To Improve Your Code( Neal Ford)guestebde
 
Automation Abstractions: Page Objects and Beyond
Automation Abstractions: Page Objects and BeyondAutomation Abstractions: Page Objects and Beyond
Automation Abstractions: Page Objects and BeyondTechWell
 

Ähnlich wie Mock driven development for .NET (20)

ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
TechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - Trivadis
TechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - TrivadisTechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - Trivadis
TechEvent 2019: Artificial Intelligence in Dev & Ops; Martin Luckow - Trivadis
 
Architecting a Large Software Project - Lessons Learned
Architecting a Large Software Project - Lessons LearnedArchitecting a Large Software Project - Lessons Learned
Architecting a Large Software Project - Lessons Learned
 
Django in the Real World
Django in the Real WorldDjango in the Real World
Django in the Real World
 
10 Ways To Improve Your Code
10 Ways To Improve Your Code10 Ways To Improve Your Code
10 Ways To Improve Your Code
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO Design
 
DataEngConf SF16 - Methods for Content Relevance at LinkedIn
DataEngConf SF16 - Methods for Content Relevance at LinkedInDataEngConf SF16 - Methods for Content Relevance at LinkedIn
DataEngConf SF16 - Methods for Content Relevance at LinkedIn
 
Mayur_Resume (2) (1)
Mayur_Resume (2) (1)Mayur_Resume (2) (1)
Mayur_Resume (2) (1)
 
Open, Secure & Transparent AI Pipelines
Open, Secure & Transparent AI PipelinesOpen, Secure & Transparent AI Pipelines
Open, Secure & Transparent AI Pipelines
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
Technologies for startup
Technologies for startupTechnologies for startup
Technologies for startup
 
Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...
Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...
Agile Mumbai 2022 - Rohit Handa | Combining Human and Artificial Intelligence...
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Latest trends in information technology
Latest trends in information technologyLatest trends in information technology
Latest trends in information technology
 
10 Ways To Improve Your Code( Neal Ford)
10  Ways To  Improve  Your  Code( Neal  Ford)10  Ways To  Improve  Your  Code( Neal  Ford)
10 Ways To Improve Your Code( Neal Ford)
 
Automation Abstractions: Page Objects and Beyond
Automation Abstractions: Page Objects and BeyondAutomation Abstractions: Page Objects and Beyond
Automation Abstractions: Page Objects and Beyond
 

Kürzlich hochgeladen

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 

Kürzlich hochgeladen (20)

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 

Mock driven development for .NET

  • 1. Mock driven development (with .NET) Test Driven Development with Mocks for .NET Framework By Punit Ganshani
  • 2. Twitter - @ganshani @codetails LinkedIn - Connect Blog Sites – www.ganshani.com www.codetails.com Open Source – CodeInject, Workflow Extractor, dI.Hook Punit Ganshani Punit Ganshani is a Technology Evangelist practising application design & development on C#, WCF, WP8, WinRT and Open Source. He is an open-source contributor on CodePlex, has several applications on Windows Phone Store, author of a book, and 18+ whitepapers in international magazines.
  • 3. Not in scope… AGILE MANIFESTO SCRUM KANBAN TFD vs. TAD
  • 4. Test Driven Development (in a nutshell) A methodology that states that test cases should be driving the design decisions and development advocates knowing the expected behaviour of our application (or a part of it), before we write the code enforces incremental design and allows us to write just enough code to pass our test Test first Development vs Test after development Doing TDD well
  • 5.
  • 6. Mock? Simulated objects that mimic the behavior of real objects in controlled ways -Wikipedia Mock – to deceive, delude, or disappoint. - English Definition
  • 7. The terminology Mocks, Stubs, … ▪ Mocks - Mock objects are used to define expectations i.e: In this scenario I expect method A() to be called with such and such parameters. Mocks record and verify such expectations.. ▪ Stubs - Stubs, on the other hand have a different purpose: they do not record or verify expectations, but rather allow us to “replace” the behaviour, state of the “fake”object in order to utilize a test scenario. ▪ Dummy - objects are passed around but never actually used. Usually they are just used to fill parameter lists. Source: http://martinfowler.com/articles/mocksArentStubs.html
  • 8. Why mock? • Actual class does not exist - Multiple development streams focusing on different components • Trips to external services, databases take a lot of time to execute • Non-existent or un-available dependent environments • A lot of UI input required in real-code • Abnormal, Unexpected conditions – outages, natural calamities, etc • Isolate code under test and narrow the scope
  • 9. When to mock? ▪ Unit Tests ▪ Mock the availability of ▪ external components ▪ non-existent classes ▪ services ▪ database calls ▪ Isolate the code under test and make it less fragile ▪ Integration Tests ▪ Do not mock your dependencies, service calls, database calls, etc ▪ Focus on checking the integration of all components ▪ Should not have any business-specific code
  • 10. Types of mocks? ▪ Strict ▪ Creates brittle tests ▪ Throws an exception if you try to use any method that has not explicitly been set up to be used. ▪ Normal, Loose ▪ Does not throw an exception if you try to use a method that is not set up, it will simply return a default value from the method and keep going.
  • 11. How to mock – Existing Applications? Single Responsibility Principle (SRP) - an object should have only a 1 S single responsibility Open/Closed Principle (OCP) - open for extension, but closed for O modification Liskov Substitution Principle (LSP) - derived class should not break L assumptions or behavior of base class Interface Segregation Principle (ISP) - many specific interfaces are 2 I better than one general purpose interface (e.g. role interface) Dependency Inversion Principle (DIP) - depend upon abstractions, not 3 D upon concretions Source: http://prasadhonrao.com/tag/solid/
  • 12. The steps of mocking Arrange Act Assert • Data setup • Mock setup Execute the Verify the unit test output
  • 13. Arrange : Object & Property Mocks RhinoMocks Moq NSubstitute
  • 14. Arrange : Method Mocks RhinoMocks Moq NSubstitute
  • 16. Beware! ▪ Remember what is mocked in which test – structure your unit tests appropriately ▪ Apply mocks only on Unit Tests, not integration tests ▪ Forces IoC container based design in and outside tests ▪ Dependencies (interfaces for mocks) should not have constructors ▪ GUI’s can’t be mocked, business logic should be Hybrid approach can be followed – Mock some things that are time consuming, external to your system, unreliable, not easily reachable and use real instances of others
  • 17.
  • 18. Further Read ▪ Understanding Mock and frameworks – Part 1 of N – Understanding the need of TDD, Mock and getting your application Unit Test ready ▪ Understanding Mock and frameworks – Part 2 of N – Understanding Mock Stages & Frameworks – Rhino Mocks, NSubstitute and Moq ▪ Understanding Mock and frameworks – Part 3 of N - Understanding how to Mock Methods calls and their Output – Rhino Mocks, NSubstitute and Moq ▪ Understanding Mock and frameworks – Part 4 of N - Understanding how to Mock Exceptions and Event Subscriptions – Rhino Mocks, NSubstitute and Moq