+ All Categories
Home > Documents > Moq presentation

Moq presentation

Date post: 15-Jul-2015
Category:
Upload: lynxstar
View: 68 times
Download: 0 times
Share this document with a friend
Popular Tags:
22
TESTING AND MOCKING WITH THE MOQ FRAMEWORK BY: ARTHUR CHARLTON EMAIL: [email protected]
Transcript

TESTING AND MOCKINGWITH THE MOQ FRAMEWORK

BY: ARTHUR CHARLTON

EMAIL: [email protected]

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


Recommended