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

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 • Assistsin 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 • .NETMocking Framework • Handles mocking boilerplate • Gives you powerful assertion tools • Can mock both abstraction and virtual concretions
  • 8.
  • 9.
    VERIFY • Usedto 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
  • 11.
  • 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
  • 13.
  • 14.
  • 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 • Arbitraryblock 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
  • 17.
  • 18.
    THE POWER OFIT • It is a special class that is a part of MoQ • Allows you to configure mocked method arguments in a generic manner
  • 19.
  • 20.
  • 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 • Anyquestions? • Time to show off some real code running with MoQ