moq presentation
DESCRIPTION
Unit Testing with MoQ Framework in .NETTRANSCRIPT
![Page 2: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/2.jpg)
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
![Page 3: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/3.jpg)
AAA PATTERN
• Arrange – Configure testing infrastructure (mocking, DI, etc)
• Act – Execute the method under test
• Assert – Verify results
![Page 4: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/4.jpg)
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
![Page 5: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/5.jpg)
TESTING PRIORITIES
• Output
• Service Dependencies
• State
![Page 6: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/6.jpg)
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
![Page 7: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/7.jpg)
MOQ
• .NET Mocking Framework
• Handles mocking boilerplate
• Gives you powerful assertion tools
• Can mock both abstraction and virtual concretions
![Page 8: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/8.jpg)
CREATING A MOQ
![Page 9: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/9.jpg)
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
![Page 10: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/10.jpg)
VERIFY - TIMES
• The Times struct lets you specific invocation amount
restrictions when verifying.
• Exactly
• At Most
• At Least
• Between
• Once/Never as convenience
![Page 11: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/11.jpg)
VERIFIABLE - EXAMPLE
![Page 12: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/12.jpg)
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
![Page 13: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/13.jpg)
RETURNS - EXAMPLE
![Page 14: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/14.jpg)
CONTINUED
![Page 15: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/15.jpg)
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.
![Page 16: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/16.jpg)
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
![Page 17: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/17.jpg)
CALLBACKS
![Page 18: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/18.jpg)
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
![Page 19: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/19.jpg)
IT - SETUP
![Page 20: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/20.jpg)
IT- VERIFY
![Page 21: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/21.jpg)
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
![Page 22: Moq Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042602/55999aeb1a28ab800d8b467f/html5/thumbnails/22.jpg)
QUESTIONS/DEMO
• Any questions?
• Time to show off some real code running with MoQ