首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >空泡法的质谱试验

空泡法的质谱试验
EN

Stack Overflow用户
提问于 2013-08-01 12:09:35
回答 2查看 1.3K关注 0票数 0

我有一个void方法,我需要对它进行单元测试,请有人帮助我如何完成它。

代码语言:javascript
复制
[TestMethod()]
public void ProcessProductFeedTest()
{
    // TODO: Initialize to an appropriate value
    ProductDataServiceProvider target = new ProductDataServiceProvider();

    target.ProcessProductFeed();
    Assert.Inconclusive("A method that does not return a value cannot be verified.");
}

在上面的代码中,ProcessProductFeed()是一种空方法,它从SQL server DB获取一些数据并发布到TIBCO,如何编写相同的单元测试用例?

EN

回答 2

Stack Overflow用户

发布于 2013-08-01 12:11:51

那么,您应该测试数据基本上是发布到TIBCO的。

对于任何方法,您的测试都应该测试返回的值是否正确,如果主要目的是计算某一项,或者如果这是该方法的主要目的,则已经发生了适当的副作用。(当然,您还可以测试错误条件。)

在不了解TIBCO或您的体系结构的情况下,我不能真正评论您如何测试发布部分。我个人会把阅读、处理和出版这三个阶段分开--然后每个部分都可以独立于其他部分进行测试。

票数 3
EN

Stack Overflow用户

发布于 2013-08-01 12:14:08

您的课堂上的持久性和TIBCO通信。例如,您可以使用一些存储库接口与SQL server进行通信:

代码语言:javascript
复制
public interface IProductsRepository
{
    IEnumerable<Product> GetSomeProducts();
    // other members
}

以及一些与TIBCO通信的网关(我将其命名为“股票”,但您应该提供特定于业务的名称):

代码语言:javascript
复制
public interface IStockGateway
{
    void DoSomethingWithProducts(IEnumerable<Product> products);
    // other members
}

然后使您的类依赖于这些抽象。您将能够模拟它们并验证类行为:

代码语言:javascript
复制
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)。下面是使用莫克库的测试:

代码语言:javascript
复制
[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
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17993662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档