Member-only story

Getting started with Unit Testing and Moq — Part 2

Russell Hammett Jr. (Kritner)
4 min readSep 1, 2018

--

In the previous post I started on a project to start learning about unit testing, specifically with moq and for WCF services. Thus far, we have only implemented basic unit tests for a piece of code with no external dependencies, which needed no mocking.

This time, I’m going to explore mocking and testing objects relying on the IDbGetSomeNumbers and INumberFunctions interfaces. As a reminder, those interfaces and corresponding classes are defined as:

namespace RussUnitTestSample.Business.Interface
{

/// <summary>
/// Interface for number functions
/// </summary>
public interface INumberFunctions
{
/// <summary>
/// Add numbers together
/// </summary>
/// <param name="numbers">The numbers to add.
/// <returns>The sum</returns>
double AddNumbers(double[] numbers);
}

/// <summary>
/// Interface to get some numbers from the database
/// </summary>
public interface IDbGetSomeNumbers
{

/// <summary>
/// Get an array of doubles from the database
/// </summary>
/// <returns></returns>
double[] GetSomeNumbers();
}
}

And the class utilizing them:

namespace RussUnitTestSample.Business
{

/// <summary>
/// Get numbers and then add them together
/// </summary>
public class GetNumbersAndAddThem
{

#region Private
private readonly IDbGetSomeNumbers _dbGetSomeNumbers;
private readonly…

--

--

Russell Hammett Jr. (Kritner)
Russell Hammett Jr. (Kritner)

Written by Russell Hammett Jr. (Kritner)

Just a boring Application Developer/Dad. I enjoy gaming, learning new technologies, reading, and potentially other stuff. That’s about it.

No responses yet