断言与单元测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//路径:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.UnitTestFramework\v4.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
}

posted @ 2015-10-29 11:28  Alfred.Xu  阅读(304)  评论(0编辑  收藏  举报