+ All Categories
Home > Technology > Unit Testing and Mocking using MOQ

Unit Testing and Mocking using MOQ

Date post: 11-May-2015
Category:
Upload: bruce-johnson
View: 603 times
Download: 1 times
Share this document with a friend
Description:
A brief description of unit testing, with a more thorough walkthrough of how to mock using MOQ. The demo code can be found at http://1drv.ms/1kQJWhb
Popular Tags:
19
Mocking Making Writing Unit Tests Your Favorite Thing to Do
Transcript
Page 1: Unit Testing and Mocking using MOQ

MockingMaking Writing Unit Tests Your Favorite

Thing to Do

Page 2: Unit Testing and Mocking using MOQ

Unit Testing

Your Code

Test

Test

Test

Page 3: Unit Testing and Mocking using MOQ

What is Being Unit Tested?• Classes or other component • Public methods• Other methods• Occasionally

• We will refer to the unit as the System Under Test or SUT

Page 4: Unit Testing and Mocking using MOQ

The Big Three Requirements• Tests are readable • Tests are trustworthy• Tests are maintainable

Page 5: Unit Testing and Mocking using MOQ

Structure of a Unit Test[TestMethod]public void Method_Scenario_Result(){

// Set dependencies, build SUT, set // expectations

// Exercise the SUT

// Check behaviour/state of SUT

// Check that expectations have been met}

Assert

Act

Arrange

Page 6: Unit Testing and Mocking using MOQ

Structure of a Unit Test[TestMethod]public void Method_Scenario_Result(){

// Set dependencies, build SUT, set // expectations

// Exercise the SUT

// Check behaviour/state of SUT

// Check that expectations have been met}

Assert

Act

Arrange

No Logic

Page 7: Unit Testing and Mocking using MOQ

Tests Should Have No Logic

• No switches, ifs or cases• Only arrange, act, assert

• Test logic == test bugs

[Test]public void CreateNumString_TwoSimpleNumbers_ReturnsStringWithCommaBetween(){ StringCalc sc = new StringCalc(); string result = sc.CreateNumString(1, 2); Assert.AreEqual(String.Format("{0},{1}", x, y), result);}

Page 8: Unit Testing and Mocking using MOQ

What is NOT a Unit Test• Tests that require additional, external set up in order

to be run• These are really integration tests

• Tests that require end-to-end functionality• These are really functional tests

Page 9: Unit Testing and Mocking using MOQ

Assertions Types• State verification• Method changes SUT state

• Behaviour verification• Verify calls between SUT and collaborator

Page 10: Unit Testing and Mocking using MOQ

What do the tests depend on?

• Collaborators• Anything called from the System Under Test

SUT DependancyDependancy

Dependency

Page 11: Unit Testing and Mocking using MOQ

Dependency Injection• A 25-dollar term for a 5-cent concept. • Dependency injection means giving an object its

instance variables. • Via James Shore

public class Sample{   private DatabaseThingie myDatabase;

  public Sample() {     myDatabase = new DatabaseThingie();   }

  public void DoStuff() {     myDatabase.GetData();   } }

Page 12: Unit Testing and Mocking using MOQ

How do we give these instances?

• Constructor injection• Property Injection (setter injection)• Method Injection• Service Locator• Interception framework

Page 13: Unit Testing and Mocking using MOQ

Fake it so – Different Kinds of Helpers• Stub – just shut up and help me• minimal behavior

• Mock – tell me when I make a mistake• able to set expectations

• Fake – behave like the real thing, more or less• simplified implementation

• Spy – tell me what happened• record calls

Page 14: Unit Testing and Mocking using MOQ

A simple Mocking Example

Page 15: Unit Testing and Mocking using MOQ

Expecting Exceptions

Page 16: Unit Testing and Mocking using MOQ

Verifying that Calls Were Made

Page 17: Unit Testing and Mocking using MOQ

Conditional Mocking

Page 18: Unit Testing and Mocking using MOQ

Callbacks

Page 19: Unit Testing and Mocking using MOQ

Testing Exceptions


Recommended