【问题标题】:How to determine which is a SUT and which is a collaborator in unit testing?如何在单元测试中确定哪个是 SUT,哪个是协作者?
【发布时间】:2020-04-22 09:01:34
【问题描述】:

我在看书,下面是一些代码:

[Fact]
public void Purchase_succeeds_when_enough_inventory()
{
    // Arrange
    var store = new Store();
    store.AddInventory(Product.Shampoo, 10);
    var customer = new Customer();

    // Act
    bool success = customer.Purchase(store, Product.Shampoo, 5);

    // Assert
    Assert.True(success);
    Assert.Equal(5, store.GetInventory(Product.Shampoo));
}

[Fact]
public void Purchase_fails_when_not_enough_inventory()
{
    // Arrange
    var store = new Store();
    store.AddInventory(Product.Shampoo, 10);
    var customer = new Customer();

    // Act
    bool success = customer.Purchase(store, Product.Shampoo, 15);

    // Assert
    Assert.False(success);
    Assert.Equal(10, store.GetInventory(Product.Shampoo));
}

作者说CustomerSUTStore合作者

这里有点迷糊,assert阶段也测试Store,那Store不也是SUT吗?

【问题讨论】:

    标签: c# unit-testing tdd


    【解决方案1】:

    SUT 代表 System Under Test,这实际上意味着您执行 act 所针对的对象。

    在断言阶段,您正在检查您的假设,其中有一个 actual 和一个 expected 值。您正在根据预期验证实际情况。


    在上述代码中,Assert.Equal 首先接收预期值,然后接收实际值。而这里的实际价值来自商店。 这很好,因为 Purchase 方法调用表明库存应该减少,因为已经下单了

    快乐之路:
    - 给定一个包含 10 件物品的库存
    - 当我购买 5 件商品时
    - 然后将保留 5 个项目

    不愉快的道路:
    - 给定一个包含 10 件物品的库存
    - 当我尝试购买 15 件商品时
    - 然后我的购买将失败,库存保持原样。

    为了更好地强调意图,您可以像这样重写测试:

    [Fact]
    public void Purchase_succeeds_when_enough_inventory()
    {
        // Arrange
        const int initialItemCount = 10;
        const int intededPurchaseCount = 5;
    
        var store = new Store();
        var product = Product.Shampoo;
        store.AddInventory(product, initialItemCount);
        var customer = new Customer();
    
        // Act
        bool isSuccess = customer.Purchase(store, product, intededPurchaseCount );
    
        // Assert
        Assert.True(isSuccess);
        var expectedInventoryCount = initialItemCount - intededPurchaseCount;
        Assert.Equal(expectedInventoryCount, store.GetInventory(product));
    }
    
    [Fact]
    public void Purchase_fails_when_not_enough_inventory()
    {
        // Arrange
        const int initialItemCount = 10;
        const int intededPurchaseCount = 15;
    
        var store = new Store();
        var product = Product.Shampoo;
        store.AddInventory(product, initialItemCount);
        var customer = new Customer();
    
        // Act
        bool isSuccess = customer.Purchase(store, product, intededPurchaseCount);
    
        // Assert
        Assert.False(isSuccess);
        Assert.Equal(initialItemCount, store.GetInventory(product));
    }
    

    【讨论】:

      【解决方案2】:

      正如 Peter Csala 所指出的,SUT 代表被测系统。如果您将 system 一词解释为“事物的安排”,我可以看出这会造成多么混乱。在这样的解释下,我可以看到商店也可以被视为更广泛系统的一部分。

      然而,这并不是 SUT 一词通常的解释方式。通常,该术语表示您直接与之交互的单位。在面向对象编程中,这通常是您调用方法的对象。

      我通常根据它们在测试中扮演的角色来命名测试变量。因此,我通常将 SUT 变量命名为 sut

      另外,现在我们已经完成了,I don't find it necessary to use comments to denote the arrange, act, and assert phases when blank lines already make that structure clear

      以下内容更接近于我编写测试的方式。

      [Fact]
      public void Purchase_succeeds_when_enough_inventory()
      {
          var store = new Store();
          store.AddInventory(Product.Shampoo, 10);
          var sut = new Customer();
      
          bool success = sut.Purchase(store, Product.Shampoo, 5);
      
          Assert.True(success);
          Assert.Equal(5, store.GetInventory(Product.Shampoo));
      }
      
      [Fact]
      public void Purchase_fails_when_not_enough_inventory()
      {
          var store = new Store();
          store.AddInventory(Product.Shampoo, 10);
          var sut = new Customer();
      
          bool success = sut.Purchase(store, Product.Shampoo, 15);
      
          Assert.False(success);
          Assert.Equal(10, store.GetInventory(Product.Shampoo));
      }
      

      这清楚地表明哪个对象是 SUT。它是名为sut 的变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-30
        • 1970-01-01
        • 1970-01-01
        • 2016-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        相关资源
        最近更新 更多