SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
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
 
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2BizTalk360
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introductionDenis Bazhin
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaEdureka!
 
Test Automation Strategies For Agile
Test Automation Strategies For AgileTest Automation Strategies For Agile
Test Automation Strategies For AgileNaresh Jain
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PBAbhishek Yadav
 
Introduction to Test Automation
Introduction to Test AutomationIntroduction to Test Automation
Introduction to Test AutomationPekka Klärck
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing frameworkIgor Vavrish
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKEAbhishek Yadav
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 

Was ist angesagt? (20)

Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
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
 
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | Edureka
 
Test Automation Strategies For Agile
Test Automation Strategies For AgileTest Automation Strategies For Agile
Test Automation Strategies For Agile
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Introduction to Test Automation
Introduction to Test AutomationIntroduction to Test Automation
Introduction to Test Automation
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing framework
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKE
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
TestNG
TestNGTestNG
TestNG
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
Automation testing
Automation testingAutomation testing
Automation testing
 

Andere mochten auch

Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQUnit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQBruce Johnson
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NETPuneet Ghanshani
 
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
 
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
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
Trivadis TechEvent 2016 Testen wird überschätzt von Andreas Fend
Trivadis TechEvent 2016 Testen wird überschätzt von Andreas FendTrivadis TechEvent 2016 Testen wird überschätzt von Andreas Fend
Trivadis TechEvent 2016 Testen wird überschätzt von Andreas FendTrivadis
 
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 PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 
Interview skills Presentation
Interview skills PresentationInterview skills Presentation
Interview skills PresentationVikram Kerkar
 

Andere mochten auch (11)

Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQUnit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
 
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
 
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
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Trivadis TechEvent 2016 Testen wird überschätzt von Andreas Fend
Trivadis TechEvent 2016 Testen wird überschätzt von Andreas FendTrivadis TechEvent 2016 Testen wird überschätzt von Andreas Fend
Trivadis TechEvent 2016 Testen wird überschätzt von Andreas Fend
 
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
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 
Interview skills Presentation
Interview skills PresentationInterview skills Presentation
Interview skills Presentation
 

Ähnlich wie Testing and mocking with the Moq framework

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
 
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
 

Ähnlich wie Testing and mocking with the Moq framework (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
 
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
 
Unit Testing talk
Unit Testing talkUnit Testing talk
Unit Testing talk
 

Kürzlich hochgeladen

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 

Kürzlich hochgeladen (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 

Testing and mocking with the Moq framework

  • 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