SlideShare a Scribd company logo
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

More Related Content

PPTX
Unit Testing (C#)
PPTX
Unit Testing And Mocking
PPT
Manual testing concepts course 1
PDF
Test Automation
PDF
Software Testing Life Cycle (STLC) | Software Testing Tutorial | Edureka
PPTX
Types of testing
PDF
Java 8 features
PDF
JUnit & Mockito, first steps
Unit Testing (C#)
Unit Testing And Mocking
Manual testing concepts course 1
Test Automation
Software Testing Life Cycle (STLC) | Software Testing Tutorial | Edureka
Types of testing
Java 8 features
JUnit & Mockito, first steps

What's hot (20)

PDF
Test Automation
PPTX
Chapter 5 - Test Management
PPT
SonarQube Overview
PPTX
Unit tests & TDD
PDF
Test Automation - Keytorc Approach
PPT
Test automation process
PPTX
SonarQube Presentation.pptx
PDF
J2ee (java ee) design patterns and architecture
PDF
Selecting the Right Automated Testing tool
PDF
Unit testing with JUnit
PPTX
How to select the right automated testing tool
PDF
Unit and integration Testing
PDF
Software Testing Process, Testing Automation and Software Testing Trends
PPTX
Automation Testing by Selenium Web Driver
PPS
ISTQB Foundation - Chapter 2
PPTX
SonarQube - The leading platform for Continuous Code Quality
PDF
INTEGRATION TESTING
PDF
Natural Language Search in Solr
PPTX
PPTX
Istqb foundation level day 1
Test Automation
Chapter 5 - Test Management
SonarQube Overview
Unit tests & TDD
Test Automation - Keytorc Approach
Test automation process
SonarQube Presentation.pptx
J2ee (java ee) design patterns and architecture
Selecting the Right Automated Testing tool
Unit testing with JUnit
How to select the right automated testing tool
Unit and integration Testing
Software Testing Process, Testing Automation and Software Testing Trends
Automation Testing by Selenium Web Driver
ISTQB Foundation - Chapter 2
SonarQube - The leading platform for Continuous Code Quality
INTEGRATION TESTING
Natural Language Search in Solr
Istqb foundation level day 1
Ad

Viewers also liked (18)

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

Similar to Moq presentation (20)

PPTX
Unit Testng with PHP Unit - A Step by Step Training
PDF
Sva.pdf
PPTX
Testing, a pragmatic approach
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
PPTX
Level Up Your Salesforce Unit Testing
PDF
An Introduction to Unit Test Using NUnit
PPTX
Automated testing of ASP .Net Core applications
PPTX
Database Unit Testing Made Easy with VSTS
PPTX
Grails Spock Testing
PPTX
When assertthat(you).understandUnitTesting() fails
PPTX
Test talk academy apachejmeter-120521121306-phpapp02
PPTX
TDD and the Legacy Code Black Hole
PPTX
Unit testing
PPTX
Qt test framework
 
PPTX
#1 unit testing
PPT
Testing – With Mock Objects
PPTX
Episode 5 - Writing unit tests in Salesforce
PPTX
Software Engg - Wk 11 - Lec 12 - Software_Testing Part-1.pptx
PDF
Agile Software Testing the Agilogy Way
PPTX
Kill the mutants and test your tests - Roy van Rijn
Unit Testng with PHP Unit - A Step by Step Training
Sva.pdf
Testing, a pragmatic approach
Unit testing and mocking in Python - PyCon 2018 - Kenya
Level Up Your Salesforce Unit Testing
An Introduction to Unit Test Using NUnit
Automated testing of ASP .Net Core applications
Database Unit Testing Made Easy with VSTS
Grails Spock Testing
When assertthat(you).understandUnitTesting() fails
Test talk academy apachejmeter-120521121306-phpapp02
TDD and the Legacy Code Black Hole
Unit testing
Qt test framework
 
#1 unit testing
Testing – With Mock Objects
Episode 5 - Writing unit tests in Salesforce
Software Engg - Wk 11 - Lec 12 - Software_Testing Part-1.pptx
Agile Software Testing the Agilogy Way
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