2017年1月7日 星期六

[.NET Core] Unit Test with MSTest

 ASP.NET Core    Unit Test    MSTest     NSubstitute  


Introduction


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


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





Unit Test Sample

Unit Test

[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 object in SplitCounter.


Install 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


沒有留言:

張貼留言