How to add moq to a working mstest unit test

I use MSTest to write unit tests, but I was instructed to add moq to the unit test. I understand that if an integration test is performed, in which the file system is usually called, then this dense one, in essence, allows testing without actual, real "real" calls. I looked around and just need help.

I started with some basic utilities that I found and started with a basic test of them using Asserts. However, I need to go to the next step and use the MOQ.

Here is the method I'm 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() { } } } 
+4
source share
1 answer

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

You are testing the IsStringEmptyOrNull method.

Suppose this method belongs to a class called Foo (I'm just doing something like this). I will also change the method.

 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 Foo class has a panel that is introduced into the constructor. In addition, it is used in your method. If you do not want your test to actually call this method:

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

You can then use the layout to accomplish this. Here's how you 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 runs, a mock version of the dependency (in this case Bar) will be assigned to the class.

When the method calls this layout, if you have the installer to return a value, it will return that value.

In this way, you can ignore dependencies in order to focus your tests or make fun of calls to db or the file system.

As for the exact example that you mentioned, there is nothing to taunt. Your exact example is not applicable for using mocks.

Mocks is not something you would do for all of your tests.

+11
source

Source: https://habr.com/ru/post/1382771/


All Articles