SlideShare une entreprise Scribd logo
1  sur  22
TESTING AND MOCKING 
WITH THE MOQ FRAMEWORK 
BY: ARTHUR CHARLTON 
EMAIL: LYNXSTAR@KERVANA.COM
UNIT TESTS VS “UNIT TESTS” 
• Different test types 
• Unit Test – Atomic, method sized, used for TDD, isolated 
• Functional/Feature Test – Story/task requirements, isolated 
• Acceptance Test – Black box test of a piece of functionality by itself 
• Integration Test – Black box testing of the system as a whole, test 
harness
AAA PATTERN 
• Arrange – Configure testing infrastructure (mocking, DI, etc) 
• Act – Execute the method under test 
• Assert – Verify results
CODE DEPENDENCIES 
• Dependency Inversion Principle is your friend 
• Spaghetti Code is hard to test 
• Heavily coupled code is hard to isolate for unit and functional 
testing 
• Coupled code can lead to large unit tests. 
• Depend on abstractions not concretions
TESTING PRIORITIES 
• Output 
• Service Dependencies 
• State
MOCKING 
• Assists in isolating the method/class/component under test. 
• Allows you to simulate parts of the system during test 
• Isolation helps with unit test and functional test level problems
MOQ 
• .NET Mocking Framework 
• Handles mocking boilerplate 
• Gives you powerful assertion tools 
• Can mock both abstraction and virtual concretions
CREATING A MOQ
VERIFY 
• Used to assert that a moq method has been called under 
certain conditions 
• Allows you verify the amount of calls 
• Allows you to verify the parameters passed in
VERIFY - TIMES 
• The Times struct lets you specific invocation amount 
restrictions when verifying. 
• Exactly 
• At Most 
• At Least 
• Between 
• Once/Never as convenience
VERIFIABLE - EXAMPLE
SETUP - RETURNS 
• Allows you to configure what a mocked out method will return 
• Chain .Returns(delegate) onto a setup method. 
• Return delegate 
• Type of Func<ParameterType1, ParameterType2,… ReturnType> 
• Input: All method arguments 
• Output: Method output 
• Great place to use lambda expressions
RETURNS - EXAMPLE
CONTINUED
RETURN PITFALLS 
• Be careful of shortcuts. 
• Returns(SomeCollection.Count) will only be evaluated once, 
regardless of how many times the mocked method is invoked 
• Returns(() => SomeCollection.Count) will be evaluated every 
time. 
• This applies to just returning a variable too, if for some reason 
this would change in between invocations you need to use a 
delegate.
CALLBACKS 
• Arbitrary block of code to be executed every time a mocked out 
method is invoked. 
• Useful for functional testing when trying to simulate parts of 
the system. 
• Similar to returns it takes in a delegate 
• Action, matching the parameter type/order 
• No returns 
• You can chain a callback to a return
CALLBACKS
THE POWER OF IT 
• It is a special class that is a part of MoQ 
• Allows you to configure mocked method arguments in a 
generic manner
IT - SETUP
IT- VERIFY
IT – VARIATIONS 
• It.Is 
• Func<Ptype, bool> - Pass it a delegate that determines if it is a match 
• It.IsAny<Ptype> (Most commonly used) 
• Passes if parameter is the supplied type 
• It.IsIn 
• Passes if parameter is in the supplied collection 
• It.Regex 
• Passes if string parameter passes the regex, fails if not string parameter
QUESTIONS/DEMO 
• Any questions? 
• Time to show off some real code running with MoQ

Contenu connexe

Tendances

Test Process
Test ProcessTest Process
Test Processtokarthik
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytestviniciusban
 
Moq Presentation
Moq PresentationMoq Presentation
Moq PresentationLynxStar
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
ISTQB Foundation Level Basic
ISTQB Foundation Level BasicISTQB Foundation Level Basic
ISTQB Foundation Level BasicErol Selitektay
 
Pruebas Automatizadas
Pruebas AutomatizadasPruebas Automatizadas
Pruebas AutomatizadasAngel Nuñez
 
테스트자동화와 TDD
테스트자동화와 TDD테스트자동화와 TDD
테스트자동화와 TDDSunghyouk Bae
 
INTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFL
INTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFLINTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFL
INTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFLRahul R Pandya
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnitkleinron
 
Quality Assurance and Software Testing
Quality Assurance and Software TestingQuality Assurance and Software Testing
Quality Assurance and Software Testingpingkapil
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Software Testing Metrics
Software Testing MetricsSoftware Testing Metrics
Software Testing MetricsVladimir Arutin
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 

Tendances (20)

Code coverage
Code coverageCode coverage
Code coverage
 
Test Process
Test ProcessTest Process
Test Process
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytest
 
Unit testing
Unit testingUnit testing
Unit testing
 
Moq Presentation
Moq PresentationMoq Presentation
Moq Presentation
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
ISTQB Foundation Level Basic
ISTQB Foundation Level BasicISTQB Foundation Level Basic
ISTQB Foundation Level Basic
 
Test Life Cycle
Test Life CycleTest Life Cycle
Test Life Cycle
 
Pruebas Automatizadas
Pruebas AutomatizadasPruebas Automatizadas
Pruebas Automatizadas
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
테스트자동화와 TDD
테스트자동화와 TDD테스트자동화와 TDD
테스트자동화와 TDD
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
TestNG
TestNGTestNG
TestNG
 
INTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFL
INTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFLINTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFL
INTRODUCTION TO ISTQB FOUNDATION LEVEL - CTFL
 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
 
Quality Assurance and Software Testing
Quality Assurance and Software TestingQuality Assurance and Software Testing
Quality Assurance and Software Testing
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Software Testing Metrics
Software Testing MetricsSoftware Testing Metrics
Software Testing Metrics
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 

En vedette

The Past Two Decades of Philippine Internet
The Past Two Decades of Philippine InternetThe Past Two Decades of Philippine Internet
The Past Two Decades of Philippine Internetgutierreziris
 
Assistive Technology - Module 4 gwen kenny
Assistive Technology - Module 4 gwen kennyAssistive Technology - Module 4 gwen kenny
Assistive Technology - Module 4 gwen kennygkenny1
 
Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?
Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?
Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?Ali Osman Öncel
 
توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية
توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية
توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية Mohamed EL Zayat
 
Reinventing Writing April 2015 Edition #ucet15
Reinventing Writing April 2015 Edition #ucet15Reinventing Writing April 2015 Edition #ucet15
Reinventing Writing April 2015 Edition #ucet15Vicki Davis
 
B1a2 symbols in dreams
B1a2 symbols in dreamsB1a2 symbols in dreams
B1a2 symbols in dreamsAarono1979
 
Glass Expo Northeast Presentation Slides - Social Media
Glass Expo Northeast Presentation Slides - Social MediaGlass Expo Northeast Presentation Slides - Social Media
Glass Expo Northeast Presentation Slides - Social MediaNJL Sales Training
 
Informaatika didaktika 2.loeng: informaatika ainekava
Informaatika didaktika 2.loeng: informaatika ainekavaInformaatika didaktika 2.loeng: informaatika ainekava
Informaatika didaktika 2.loeng: informaatika ainekavaMart Laanpere
 
Augmented reality browsers a proposal for architectural standardization
Augmented reality browsers  a proposal for architectural standardizationAugmented reality browsers  a proposal for architectural standardization
Augmented reality browsers a proposal for architectural standardizationijseajournal
 
Sample paper for maths 12th class uk board
Sample paper for maths   12th class uk boardSample paper for maths   12th class uk board
Sample paper for maths 12th class uk boardsonamalhotra
 
Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...
Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...
Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...SERC at Carleton College
 
A model for run time software architecture adaptation
A model for run time software architecture adaptationA model for run time software architecture adaptation
A model for run time software architecture adaptationijseajournal
 
Carlton Hobbs: Development of Wax Sculpture
Carlton Hobbs: Development of Wax SculptureCarlton Hobbs: Development of Wax Sculpture
Carlton Hobbs: Development of Wax SculptureCarlton Hobbs
 
Survey paper on Detecting Blackhole Attack by different Approaches and its Co...
Survey paper on Detecting Blackhole Attack by different Approaches and its Co...Survey paper on Detecting Blackhole Attack by different Approaches and its Co...
Survey paper on Detecting Blackhole Attack by different Approaches and its Co...IJARIIE JOURNAL
 
Npulp sept 2012 with videos
Npulp sept 2012 with videosNpulp sept 2012 with videos
Npulp sept 2012 with videosKevin Williams
 

En vedette (18)

The Past Two Decades of Philippine Internet
The Past Two Decades of Philippine InternetThe Past Two Decades of Philippine Internet
The Past Two Decades of Philippine Internet
 
Assistive Technology - Module 4 gwen kenny
Assistive Technology - Module 4 gwen kennyAssistive Technology - Module 4 gwen kenny
Assistive Technology - Module 4 gwen kenny
 
Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?
Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?
Deprem Konferansı Teklif Dilekçesi Nasıl Yazılır?
 
توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية
توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية
توظيف شبكات التواصل الاجتماعى لمتابعة برامج ما بعد محو الأمية
 
Reinventing Writing April 2015 Edition #ucet15
Reinventing Writing April 2015 Edition #ucet15Reinventing Writing April 2015 Edition #ucet15
Reinventing Writing April 2015 Edition #ucet15
 
B1a2 symbols in dreams
B1a2 symbols in dreamsB1a2 symbols in dreams
B1a2 symbols in dreams
 
Cherk_kray_91
Cherk_kray_91Cherk_kray_91
Cherk_kray_91
 
Glass Expo Northeast Presentation Slides - Social Media
Glass Expo Northeast Presentation Slides - Social MediaGlass Expo Northeast Presentation Slides - Social Media
Glass Expo Northeast Presentation Slides - Social Media
 
Informaatika didaktika 2.loeng: informaatika ainekava
Informaatika didaktika 2.loeng: informaatika ainekavaInformaatika didaktika 2.loeng: informaatika ainekava
Informaatika didaktika 2.loeng: informaatika ainekava
 
B05310610
B05310610B05310610
B05310610
 
Augmented reality browsers a proposal for architectural standardization
Augmented reality browsers  a proposal for architectural standardizationAugmented reality browsers  a proposal for architectural standardization
Augmented reality browsers a proposal for architectural standardization
 
Sample paper for maths 12th class uk board
Sample paper for maths   12th class uk boardSample paper for maths   12th class uk board
Sample paper for maths 12th class uk board
 
Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...
Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...
Introduction to InTeGrate Modules: Hands-on, data-rich, and socially-relevant...
 
A05320107
A05320107A05320107
A05320107
 
A model for run time software architecture adaptation
A model for run time software architecture adaptationA model for run time software architecture adaptation
A model for run time software architecture adaptation
 
Carlton Hobbs: Development of Wax Sculpture
Carlton Hobbs: Development of Wax SculptureCarlton Hobbs: Development of Wax Sculpture
Carlton Hobbs: Development of Wax Sculpture
 
Survey paper on Detecting Blackhole Attack by different Approaches and its Co...
Survey paper on Detecting Blackhole Attack by different Approaches and its Co...Survey paper on Detecting Blackhole Attack by different Approaches and its Co...
Survey paper on Detecting Blackhole Attack by different Approaches and its Co...
 
Npulp sept 2012 with videos
Npulp sept 2012 with videosNpulp sept 2012 with videos
Npulp sept 2012 with videos
 

Similaire à Moq presentation

Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingRam Awadh Prasad, PMP
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approachEnrico Da Ros
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingGordon Bockus
 
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
 
Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications nispas
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSSanil Mhatre
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsMartin Skurla
 
Test talk academy apachejmeter-120521121306-phpapp02
Test talk academy apachejmeter-120521121306-phpapp02Test talk academy apachejmeter-120521121306-phpapp02
Test talk academy apachejmeter-120521121306-phpapp02veeru madduri
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleNoam Kfir
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
#1 unit testing
#1 unit testing#1 unit testing
#1 unit testingeleksdev
 
Testing – With Mock Objects
Testing – With Mock ObjectsTesting – With Mock Objects
Testing – With Mock Objectsemmettwalsh
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceJitendra Zaa
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayJordi Pradel
 
Kill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsKill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsRoy van Rijn
 
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnKill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnNLJUG
 

Similaire à Moq presentation (20)

Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Sva.pdf
Sva.pdfSva.pdf
Sva.pdf
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit Testing
 
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
 
Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTS
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() fails
 
Test talk academy apachejmeter-120521121306-phpapp02
Test talk academy apachejmeter-120521121306-phpapp02Test talk academy apachejmeter-120521121306-phpapp02
Test talk academy apachejmeter-120521121306-phpapp02
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Unit testing
Unit testingUnit testing
Unit testing
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
#1 unit testing
#1 unit testing#1 unit testing
#1 unit testing
 
Testing – With Mock Objects
Testing – With Mock ObjectsTesting – With Mock Objects
Testing – With Mock Objects
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy Way
 
Kill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsKill the mutants - A better way to test your tests
Kill the mutants - A better way to test your tests
 
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnKill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van Rijn
 

Moq presentation

  • 1. TESTING AND MOCKING WITH THE MOQ FRAMEWORK BY: ARTHUR CHARLTON EMAIL: LYNXSTAR@KERVANA.COM
  • 2. UNIT TESTS VS “UNIT TESTS” • Different test types • Unit Test – Atomic, method sized, used for TDD, isolated • Functional/Feature Test – Story/task requirements, isolated • Acceptance Test – Black box test of a piece of functionality by itself • Integration Test – Black box testing of the system as a whole, test harness
  • 3. AAA PATTERN • Arrange – Configure testing infrastructure (mocking, DI, etc) • Act – Execute the method under test • Assert – Verify results
  • 4. CODE DEPENDENCIES • Dependency Inversion Principle is your friend • Spaghetti Code is hard to test • Heavily coupled code is hard to isolate for unit and functional testing • Coupled code can lead to large unit tests. • Depend on abstractions not concretions
  • 5. TESTING PRIORITIES • Output • Service Dependencies • State
  • 6. MOCKING • Assists in isolating the method/class/component under test. • Allows you to simulate parts of the system during test • Isolation helps with unit test and functional test level problems
  • 7. MOQ • .NET Mocking Framework • Handles mocking boilerplate • Gives you powerful assertion tools • Can mock both abstraction and virtual concretions
  • 9. VERIFY • Used to assert that a moq method has been called under certain conditions • Allows you verify the amount of calls • Allows you to verify the parameters passed in
  • 10. VERIFY - TIMES • The Times struct lets you specific invocation amount restrictions when verifying. • Exactly • At Most • At Least • Between • Once/Never as convenience
  • 12. SETUP - RETURNS • Allows you to configure what a mocked out method will return • Chain .Returns(delegate) onto a setup method. • Return delegate • Type of Func<ParameterType1, ParameterType2,… ReturnType> • Input: All method arguments • Output: Method output • Great place to use lambda expressions
  • 15. RETURN PITFALLS • Be careful of shortcuts. • Returns(SomeCollection.Count) will only be evaluated once, regardless of how many times the mocked method is invoked • Returns(() => SomeCollection.Count) will be evaluated every time. • This applies to just returning a variable too, if for some reason this would change in between invocations you need to use a delegate.
  • 16. CALLBACKS • Arbitrary block of code to be executed every time a mocked out method is invoked. • Useful for functional testing when trying to simulate parts of the system. • Similar to returns it takes in a delegate • Action, matching the parameter type/order • No returns • You can chain a callback to a return
  • 18. THE POWER OF IT • It is a special class that is a part of MoQ • Allows you to configure mocked method arguments in a generic manner
  • 21. IT – VARIATIONS • It.Is • Func<Ptype, bool> - Pass it a delegate that determines if it is a match • It.IsAny<Ptype> (Most commonly used) • Passes if parameter is the supplied type • It.IsIn • Passes if parameter is in the supplied collection • It.Regex • Passes if string parameter passes the regex, fails if not string parameter
  • 22. QUESTIONS/DEMO • Any questions? • Time to show off some real code running with MoQ