您班级的抽象持久性和 TIBCO 通信。例如,您可以使用一些存储库接口与 SQL 服务器进行通信:
public interface IProductsRepository
{
IEnumerable<Product> GetSomeProducts();
// other members
}
还有一些用于与 TIBCO 通信的网关(我将其命名为 Stock,但您应该提供特定于业务的名称):
public interface IStockGateway
{
void DoSomethingWithProducts(IEnumerable<Product> products);
// other members
}
然后让你的类依赖这些抽象。您将能够模拟它们并验证类行为:
public class ProductDataServiceProvider
{
private IProductsRepository _productRepository;
private IStockGateway _stockGateway;
// inject implementations
public ProductDataServiceProvider(
IProductRepository productRepository,
IStockGateway stockGateway)
{
_productRepository = productRepository;
_stockGateway = stockGateway;
}
public void ProcessProductFeed()
{
// use repository and gateway
}
}
现在,回到测试。您的提供商的职责是什么 - 从产品存储库中获取一些产品(此存储库的实现将从 SQL 数据库加载产品)并将它们传递给网关(网关的实现会将产品发布到 TIBCO)。这是使用Moq库的测试:
[TestMethod]
public void ShouldPassSomeProjectToStock()
{
// Arrange
var products = new List<Product>() { }; // create some products
var mockRepository = new Mock<IProductRepository>();
mockRepository.Setup(r => r.GetSomeProducts()).Returns(products);
var mockGateway = new Mock<IStockGateway>();
mockGateway.Setup(g => g.DoSomethingWithProducts(products));
var provider = new ProductDataServiceProvider(mockRepository.Object,
ockGateway.Object);
// Act
provider.ProcessProductFeed();
// Assert
mockRepository.VerifyAll(); // verify products retrieved from repository
mockGateway.VerifyAll(); // verify products passed to gateway
}