1. 程式人生 > 實用技巧 >C#學習筆記-鐵錳C#入門 視訊P29 介面 例子4-5(介面 單元測試+Moq)

C#學習筆記-鐵錳C#入門 視訊P29 介面 例子4-5(介面 單元測試+Moq)

 1 using System;
 2 using ConsoleApp24;
 3 using Microsoft.VisualStudio.TestTools.UnitTesting;
 4 using Moq;
 5 
 6 namespace UnitTestProject1
 7 {
 8     [TestClass]
 9     public class DaskFanTest1
10     {
11         [TestMethod]  //特性
12                       //[testm]
13 
14         public void PowerHightOK()
15 { 16 //var mock = new Mock<iPowerSupply>(); 17 //mock.Setup(ps => ps.GetPower()).Returns(() => 220); 18 //var fan = new DaskFan(mock.Object); 19 20 var fan = new DaskFan(new PowerSupplylT200()); 21 var expected = "warring!!";
22 var actual = fan.Work(); 23 Assert.Equals(expected, actual); 24 25 } 26 27 public void PowerLowerOK() 28 { 29 //var mock = new Mock<iPowerSupply>(); 30 //mock.Setup(ps => ps.GetPower()).Returns(() => 0); 31 //
var fan = new DaskFan(mock.Object); 32 33 var fan = new DaskFan(new PowerSupplylTZero()); 34 var expected = "Won't work!"; 35 var actual = fan.Work(); 36 Assert.Equals(expected, actual); 37 38 } 39 40 41 } 42 43 class PowerSupplylTZero : iPowerSupply 44 { 45 public int GetPower() 46 { 47 return 0; 48 } 49 } 50 51 class PowerSupplylT200 : iPowerSupply 52 { 53 public int GetPower() 54 { 55 return 220; 56 } 57 } 58 }