[UnitTest]使用FluentAssertions來驗證單元測試

FluentAssertions是很好用的套件

他讓單元測試的程式碼變的更簡潔明瞭

 

第一步先從Nuget安裝

如果你習慣用NUnit他也有支援、Autofac也是、這次我們只用最簡單的方式搭配VS內建的MSTest做

 

Fluent Assertions官方的文件寫的很清楚

我只列出我常用的部份給大家參考

using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using System;
using System.Collections.Generic;
using FluentAssertions.Extensions;
using System.Linq;

namespace MySample.Controllers.Tests
{
    [TestClass()]
    public class HomeControllerTests
    {
        [TestMethod()]
        public void SampleTest()
        {
            //文字------------------------------------------------------------
            string test1 = "ABC";

            test1.Should().Be("ABC");
            test1.Should().NotBe("DEF");
            test1.Should().BeEquivalentTo("aBc", "因為大小寫我不在意");

            test1.Should().Contain("A");
            test1.Should().NotContain("D");
            test1.Should().ContainEquivalentOf("a", "因為大小寫我不在意");
            test1.Should().NotContainEquivalentOf("d");

            test1.Should().NotBeNull();
            test1.Should().BeNull("因為值是 ABC 不是 null");
            test1.Should().BeEmpty("因為值是 ABC 不是 空");
            test1.Should().NotBeEmpty();
            test1.Should().HaveLength(3);
            test1.Should().BeNullOrWhiteSpace("因為值是 ABC");
            test1.Should().NotBeNullOrWhiteSpace();

            test1.Should().Match("*B*", "支援萬用字元");
            test1.Should().MatchEquivalentOf("*b*");

            //布林-----------------------------------------------------------
            bool test2 = true;

            test2.Should().BeTrue();
            test2.Should().BeFalse("因為是 True");

            //數字-----------------------------------------------------------
            int test3 = 80;

            test3.Should().Be(80);
            test3.Should().BeInRange(0, 100, "分數只會有 0 ~ 100 分");
            test3.Should().BeGreaterOrEqualTo(60, "60 分才及格");
            test3.Should().BePositive("考試成績不倒扣,不會有負數");

            //日期-----------------------------------------------------------
            DateTime test4 = DateTime.Parse("2001/12/24 19:30");

            test4.Should().BeAfter(24.December(2001));
            test4.Should().Be(25.December(2001).At(19, 30), "聖誕節是12/25,不是12/24");


            //集合 (最好用的地方)--------------------------------------------
            List<Student> test5 = GetStudents("MyId", "John");
            List<Student> test5_assert = new List<Student>
            {
                new Student {Id = "MyId_1", Name = "John_1" },
                new Student {Id = "MyId_2", Name = "John_2" },
                new Student {Id = "MyId_3", Name = "John_3" },
            };

            test5.Should().NotBeEmpty();
            test5.Should().HaveCount(3);
            test5.Should().HaveCount(q => q >= 1, "Lambda 運算式也支援");
            test5.First().Should().Equals(new Student { Id = "MyId_1", Name = "John_1" });
            test5.Should().BeEquivalentTo(test5_assert);     //比對集合內物件的所有欄位 超強大!!!
        }

        /// <summary>
        /// 為了方便講解
        /// </summary>
        private List<Student> GetStudents(string id, string name)
        {
            var result = new List<Student>
            {
                new Student {Id = id + "_1", Name = name + "_1" },
                new Student {Id = id + "_2", Name = name + "_2" },
                new Student {Id = id + "_3", Name = name + "_3" },
            };

            return result;
        }
    }

    /// <summary>
    /// 範例
    /// </summary>
    class Student
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}

 

然後你可能會疑惑我一直在Assert後面加上一個像解釋的字串  例:"分數只會有 0 ~ 100 分"  有什麼意義?

因為如果我故意讓test case沒過 他會變成像這樣

 

想知道更清楚了解可以看官方文件