Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add moq to a working mstest unit test

Tags:

mstest

moq

I am using MSTest to write unit tests, but I have been tasked with adding moq to the unit test. I understand that if an integration test is done in which a file system, a database is normally called, that mocking is essentially allowing for the testing without actually making those "real" calls. I have looked around and just need some help.

I was starting with some basic utilities that I found and started with some basic testing of them with Asserts. However, I'm needing to take it to the next step and leverage MOQ.

Here is a method that I am testing:

    public static bool IsStringEmptyOrNull(string strValue)
    {
        if(null != strValue)
        {
            strValue = strValue.Trim().ToLower();
            return (string.Empty == strValue || "undefined" == strValue);
        }
        return true;
    }

Then I have a Test that looks like this:

  using System;
  using System.Text;
  using System.Collections.Generic;
  using System.Linq;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using Company.W.Utilities;


namespace CESUtilities.Tests
{
[TestClass]
public class When_string_is_empty_or_null
{
    private string empty;
    private string isnull;
    private bool expected;

    [TestInitialize()]
    public void TestInitialize()
    {
        empty = "";
        isnull = null;
        expected = true;
    }

    [TestMethod]
    public void when_string_is_empty()
    {

        bool actual = Util.IsStringEmptyOrNull(empty);   
        Assert.AreEqual(expected, actual);
    }

    [TestMethod]
    public void when_string_is_null()
    {
        bool actual = Util.IsStringEmptyOrNull(isnull);
        Assert.AreEqual(expected, actual);
    }



    [TestCleanup()]
    public void TestCleanup()
    {

    }


  }
}
like image 460
Tom Stickel Avatar asked Dec 13 '22 07:12

Tom Stickel


1 Answers

First of all, as you mentioned you can replace dependencies with mocks. The code you have posted does not contain much in terms of dependencies, but let's pretend it does.

You are testing the method IsStringEmptyOrNull.

Let's suppose that method belongs to a class named Foo (I'm just making stuff up). I'll change the method a bit too.

public class Foo
{
    private IBar _bar;

    public Foo(IBar bar)
    {
        _bar = bar; 
    }   

    public static bool IsStringEmptyOrNull(string strValue)
    {
        // dependency is called here
        var readValue = bar.ReadFromFileSystemOrDatabase();

        if(null != strValue 
           && readValue == 1)
        {
            strValue = strValue.Trim().ToLower();
            return (string.Empty == strValue || "undefined" == strValue);
        }
        return true;
    }
}

Here you can see that the class Foo has a Bar which is injected in the constructor. Also, it is used in your method. If you do no want your test to actually call this method:

  • because it calls to db
  • because it calls to filesystem
  • you want to isolate the code from the method, from other external code (in this case ReadFromFileSystemOrDatabase()

You can then use a mock to acomplish this. Here is how you would do it:

// create your mock object
var mockBar = new Mock<IBar>();

// setup a method to return a result when the mock object is called
// notice the return value, which is 1
mockBar.Setup(mock => mock.ReadFromFileSystemOrDatabase()).Returns(1);

// you can then inject the mock object in the object you are testing
var foo = new Foo(mockBar.Object);

What will happen is that when your test is run, a mock version of the dependency (in this case Bar) will be given to the class.

When the method will make a call on this mock, if you have Setup the method to return a value, it will return this value.

This way you can abstract away dependencies to focus your tests or mock away calls to a db or file system.

As for the exact example you mentioned there isn't anything to mock. Your exact example is not applicable to using mocks.

Mocks aren't something you would do for all your tests.

like image 74
Gilles Avatar answered Dec 29 '22 09:12

Gilles