iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 25
0
Modern Web

Learning ASP.NET core + Angular2 系列 第 25

[.NET Core] Unit Test with MSTest

Introduction


So how to choose our unit test framework? Take a look at this discussion,

NUnit vs. MbUnit vs. MSTest vs. xUnit.net

In this article, we will use MSTest for example, and learn how to use NSubstitute for mocking and decoupling.

Environment


  • Visual Studio 2015 Update 3
  • .NET Core : 1.0.0
  • dotnet-test-mstest : 1.1.2-preview
  • MSTest.TestFramework : 1.0.8-rc

Implement


Install packages

  1. dotnet-test-mstest
  2. MSTest.TestFramework

Unit test sample

[TestClass]
public class UnitTestDemo
{
        [TestMethod]
        public void TestSplitCount()
        {
            var input = "Luke Skywalker, Leia Skywalker, Anakin Skywalker";
            var expected = 3;
            var actual = input.Split(',').Count();
            Assert.AreEqual(expected, actual);
        }
}
  • Test result

Using NSubstitute

Assume that we have a class, SplitCounter, which load the separator from configuration file (appsettings.json) as following,

  • SplitCounter.cs
public class SplitCounter
    {
        public ISplitConfig _splitConfig = null;

        public SplitCounter()
        {
            this._splitConfig = new SplitConfig();
        }

        public int Calculate(string input)
        {
            if (this._splitConfig == null)
            {
                throw new NullReferenceException("SplitConfig is null!");
            }
            else
            {
                return input.Split(this._splitConfig.Separator).Count();
            }
        }
    }
  • ISplitConfig.cs
public interface ISplitConfig
{
        char[] Separator { get; set; }
}
  • SplitConfig.cs
public class SplitConfig : ISplitConfig
{
        public char[] Separator { get; set; }

        public SplitConfig()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");

            var configuration = builder.Build();

            var separator = configuration["Separator"];
            this.Separator = separator.ToCharArray();
        }
}
  • appsettings.json
{
  "Separator": ","
}

However, we don’t want the SplitCounter instance rely on anything from configuration file when unit-testing.

Let us use NSubstitute to mock ISplitConfig instance in SplitCounter.

Install NSubstitute

  1. NSubstitute

Unit Test

[TestMethod]
public void TestSplitCounterWithNSub()
{
            #region Create the Substitutes
            var splitConfig = Substitute.For<ISplitConfig>();
            #endregion

            #region Set the return value of the mock objects
            splitConfig.Separator.Returns(new char[] { ';' });
            #endregion

            #region Intialize the test target
            var splitCounter = new SplitCounter();
            splitCounter._splitConfig = splitConfig;
            #endregion


            var input = "Luke Skywalker;Leia Skywalker;AnakinSkywalker";
            var expected = 3;
            var actual = splitCounter.Calculate(input);
            Assert.AreEqual(expected, actual);
}
  • Test Result

Reference


  1. Announcing MSTest V2 Framework support for .NET Core 1.0 RTM

上一篇
[.NET Core] Unit Test with xUnit.net
下一篇
[Angular2] Redux with ngrx/store - Intro
系列文
Learning ASP.NET core + Angular2 31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言