.NET????????????????1??
???????????? ???????[ 2013/10/9 13:47:19 ] ????????
??????????????????????????????????OrderService??????????????GetOrders??????????????????????????????????????????????????ж???????????????????????????Mock????
?????1??
?????????????????£?????????????????????ó??????????????????????????????????
?????2??
???????????????????????????????????С??????????????????????????乲?????Mock?????????????????????????????OrderService_GetOrders_NormalFlows?????У???IServiceConnection????????????????Mock???????е?OrderService_GetOrders_OrderIdIsNull??????????????????????ε?Mock?????????????????????£?????????????????????????????????????????????????????????????????????????????????棬????????????????????乲???????????????????????????????????????????????
??????????????μ?????????????2??????????
namespace UnitTestRefactoring { public class ProductService { private IServiceConnection ServiceConnection; private IServiceReader ServiceReader; private IServiceWriter ServiceWrite; public ProductService(IServiceConnection connection?? IServiceReader reader?? IServiceWriter writer) { this.ServiceConnection = connection; this.ServiceReader = reader; this.ServiceWrite = writer; } public bool GetProduct(string productId) { if (string.IsNullOrWhiteSpace(productId)) return false; return true; } } } |
namespace UnitTestRefactoring { public class ProductService { private IServiceConnection ServiceConnection; private IServiceReader ServiceReader; private IServiceWriter ServiceWrite; public ProductService(IServiceConnection connection?? IServiceReader reader?? IServiceWriter writer) { this.ServiceConnection = connection; this.ServiceReader = reader; this.ServiceWrite = writer; } public bool GetProduct(string productId) { if (string.IsNullOrWhiteSpace(productId)) return false; return true; } } } |
???????????Product?????????????????????OrderService?????????б??????????????GetProduct??????
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using NSubstitute; using UnitTestRefactoring; namespace UnitTestRefactoring.UnitTests { [TestClass] public class ProductService_UnitTests { IServiceConnection mockServiceConnection = Substitute.For<IServiceConnection>(); IServiceReader mockServiceReader = Substitute.For<IServiceReader>(); IServiceWriter mockServiceWriter = Substitute.For<IServiceWriter>(); [TestMethod] public void ProductService_GetProduct_NormalFlows() { ProductService testProductService = new ProductService(mockServiceConnection?? mockServiceReader?? mockServiceWriter); bool testResult = testProductService.GetProduct("5475684684"); Assert.AreEqual(true?? testResult); } [TestMethod] public void ProductService_GetProduct_ProductIsNull() { ProductService testProductService = new ProductService(mockServiceConnection?? mockServiceReader?? mockServiceWriter); bool testResult = testProductService.GetProduct(string.Empty); Assert.AreEqual(false?? testResult); } } } |
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11