The document discusses testing and mocking with the Moq framework. It covers different types of tests like unit tests, integration tests, and acceptance tests. It also discusses AAA pattern for arranging, acting, and asserting in tests. The document explains how to use Moq to mock dependencies and isolate components for testing. It provides examples of how to create mocks, verify mock calls, setup mock returns, use callbacks, and take advantage of Moq's "It" functionality.
1 of 22
Downloaded 132 times
More Related Content
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
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
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