Tuesday, April 9, 2013

Moq Framework for Unit Testing

 

 

Properties of a good Unit Test

  1. Atomic : A unit test should test a small piece of functionality
  2. Deterministic : A unit test should always either pass or fail.
  3. Repeatable: A test is repeatable when it passes consitently. If a test passes and fails without changing the test or any of the dependent code is not repeatable.
  4. Order independent and Isolated – A test should not be dependent on any other test.
  5. Fast: if a test is taking 1 second to run it is slow. It should take milisecond.
  6. Easy to Setup

 

image

A mocking framework replaces these dependencies with Fake copies of dependencies:

image

Hand rolled Mock Objects:

Fake implementations of abstractions

Used to verify class interactions in automated tests.

Created by hand writing code

Creating hand rolled mocks:

  1. Basic class
  2. Implement the dependency interface type
  3. Flush out the dependency functionality needed
    1. Return Values
    2. Exception throwing
    3. “was it called?”
    4. “How many times it called?”

AAA Syntax (Arrange Act and Assert)

Arrange

Create a mock object

Act

Execute the SUT (System under test)

Assert

Verify SUT’s interaction with the mock object

No comments: