xUnit-Moq框架
基于上一次的單元測試-xUnit進行
Models文件夾
Staff類修改為:
| public class Staff { ??? public int Id { get; set; } ??? public string Name { get; set; } ??? public string State { get; set; } ??? public int Age { get; set; } } |
創建一個IRepository接口
| public interface IRepository { IEnumerable<Staff> Staffs{ get; } } |
? ? ? ? ? ? ?創建一個SimpleRepository類實現IRepository接口
| public class SimpleRepository : IRepository { ??? public IEnumerable<Staff> Staffs=> new Staff[] ??? { ??????? new Staff(){ Id = 1, Name = "張三", State = "在職", Age = 18 }, ??????? new Staff(){ Id = 2, Name = "李四", State = "離職", Age = 20 }, ??????? new Staff(){ Id = 10, Name = "王五", State = "在職", Age = 36 }, ??????? new Staff(){ Id = 8, Name = "血友病", State = "在職", Age = 18 } ??? }; } |
這樣的好處:
Controllers文件夾
HomeController 內容修改為
| public class HomeController : Controller { ??? private IRepository repository = new SimpleRepository(); ? ??? public IRepository Repository { ??????? get => repository ; ??????? set => repository = value; ??? } ? ??? public IActionResult Index() ??? { ??????? return View(Repository.Staffs.Where(x => x.State == "在職")); ??? } } |
Staffitems.Tests項目
在NuGet管理器種添加Moq軟件包
Moq是什么? ==== 是一個用于在應用程序種創建組件的仿冒實現的軟件包
Moq有何用? ====Moq框架是的為單元測試創建仿冒組件以隔離應用程序的部件變得更容易
有什么替代品? ===== 有很多替代品,包括NSubstitue和FakeItEasy。所有這些框架都提供類似的功能,可在它們之間進行選擇
?
在HomeControllerTests類中使用Moq
????????????????紅色部分為新增或者修改
????????????????綠色部分為解釋
| public class HomeControllerTests { ??? [Fact] ??? public void IndexActionModelIsComplete2() ??? { ??????? //創建Mocking ??????? var mock = new Mock<IRepository>(); ? ??????? //SetupGet:為調用屬性getter指定假數據的設置。 ??? //當Products被讀取時返回Returns方法里的結果 ??????? mock.SetupGet(x => x.Staffs).Returns(new[] ??????? { ??????????? new Staff{ Name ="z1",Age = 5, State = "在職" }, ??????????? new Staff{ Name ="z2",Age = -3232 ,State = "在職" }, ??????????? new Staff{ Name ="z3",Age = 99999 ,State = "在職" } ??????? }); ? ??????? //獲取Home控制器,并重新設置該Repository的值 ??????? //Repository一開始指向與SimpleRepository ??????? //現在指向與我們在上面定義的假數據 ??????? var controll = new HomeController() { Repository = mock.Object }; ??? ??? //調用HomeControll中的Index()方法并獲取返回的類型 ??????? var model = (controll.Index() as ViewResult).ViewData.Model ??????????? as IEnumerable<Staff>; ??? ??? //對比預料值與實際值是否一致 ??????? Assert.Equal( ??????????? controll.Repository.Staffs.Where(x => x.State == "在職"), ??????????? model, ??????????? Comparer.Get<Staff>((a, b) => a.Name == b.Name && a.State == b.State && a.Id == b.Id) ??????? ); ??? } } |
測試Index()方法只執行了一次
| [Fact] public void IndexActionModelIsComplete3() { ??? var controller = new HomeController(); ??? var mock = new Mock<IRepository>(); ? ??? mock.SetupGet(m => m.Staffs).Returns(new[] ??? { ??????? new Staff{ Name ="z1",Age = 5, State = "在職" }, ??????? new Staff{ Name ="z2",Age = -3232 ,State = "在職" }, ??????? new Staff{ Name ="z3",Age = 99999 ,State = "在職" } ??? }); ? ??? controller.Repository = mock.Object; ? ??? //執行Index()方法 ??? var result = controller.Index(); ??? var result2 = controller.Index(); ??? var result3 = controller.Index(); ? ??? //驗證是否在模擬上讀取了Products屬性。并且只讀取了一次(Times.Once) ??? mock.VerifyGet(m => m.Staffs, Times.Once); } |
????????????????刪除以下代碼就行了
| var result2 = controller.Index(); var result3 = controller.Index(); |
總結
以上是生活随笔為你收集整理的xUnit-Moq框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Swift A] - 实战-豆瓣电台总
- 下一篇: ADC及DAC简介