zoukankan      html  css  js  c++  java
  • 断言与单元测试

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    //路径:C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.QualityTools.UnitTestFrameworkv4.0_10.0.0.0__b03f5f7f11d50a3a
    //文件:Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
    namespace Chap2_4
    {
    class Program
    {
    static void Main(string[] args)
    {
    ProductCollectionTests pct = new ProductCollectionTests();
    pct.ProductCollection_DistributeProduct_Test();
    Console.ReadKey();
    }
    }
    #region 代码逻辑
    public class ProductCollection
    {
    public List<Product> Products { get; set; }
    //根据传递进来的ids集合确定到底需要配送那些商品
    public List<Product> DistributeProduct(List<int> ids)
    {
    var result = from item in Products where ids.Contains(item.PId) select item;
    DistributeNotice notice = new DistributeNotice();
    notice.ToNotice("配送商品");
    return result.ToList();
    }
    }
    public class Product
    {
    public string PName { get; set; }
    public int PId { get; set; }
    }
    //发送配送信息
    public class DistributeNotice
    {
    public void ToNotice(string mes)
    {
    Console.WriteLine(mes);
    }
    }
    #endregion
    #region 单元测试代码
    public class ProductCollectionTests
    {
    public void ProductCollection_DistributeProduct_Test()
    {
    ProductCollection testsObject = new ProductCollection();
    testsObject.Products = new List<Product>()
    {
    new Product(){PId=1,PName="1"},
    new Product(){PId=2,PName="2"},
    new Product(){PId=3,PName="3"}
    };
    var testResult=testsObject.DistributeProduct(new List<int>(){1,2});
    //断言;如果DistributeProduct方法被改动,那么下面的代码就会报异常
    Assert.AreEqual(testResult.Count,2);
    Assert.AreEqual(testResult[0].PId, 1);
    Assert.AreEqual(testResult[1].PId, 2);
    }
    }
    #endregion
    }

  • 相关阅读:
    P1030 求先序排列 P1305 新二叉树
    spfa
    Clairewd’s message ekmp
    Cyclic Nacklace hdu3746 kmp 最小循环节
    P1233 木棍加工 dp LIS
    P1052 过河 线性dp 路径压缩
    Best Reward 拓展kmp
    Period kmp
    Substrings kmp
    Count the string kmp
  • 原文地址:https://www.cnblogs.com/sulong/p/4919924.html
Copyright © 2011-2022 走看看