??????????????????????????????????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);
}
}
}