Top Banner
TESTING AND MOCKING WITH THE MOQ FRAMEWORK BY: ARTHUR CHARLTON EMAIL: [email protected]
22
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Moq presentation

TESTING AND MOCKINGWITH THE MOQ FRAMEWORK

BY: ARTHUR CHARLTON

EMAIL: [email protected]

Page 2: Moq presentation

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

AAA PATTERN

• Arrange – Configure testing infrastructure (mocking, DI, etc)

• Act – Execute the method under test

• Assert – Verify results

Page 4: Moq presentation

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

TESTING PRIORITIES

• Output

• Service Dependencies

• State

Page 6: Moq presentation

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

MOQ

• .NET Mocking Framework

• Handles mocking boilerplate

• Gives you powerful assertion tools

• Can mock both abstraction and virtual concretions

Page 8: Moq presentation

CREATING A MOQ

Page 9: Moq presentation

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

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

VERIFIABLE - EXAMPLE

Page 12: Moq presentation

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

RETURNS - EXAMPLE

Page 14: Moq presentation

CONTINUED

Page 15: Moq presentation

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

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

CALLBACKS

Page 18: Moq presentation

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

IT - SETUP

Page 20: Moq presentation

IT- VERIFY

Page 21: Moq presentation

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

QUESTIONS/DEMO

• Any questions?

• Time to show off some real code running with MoQ