研究了一周的单元测试,现在勉强算是摸到了门槛,现整理笔记如下:
项目架构是简单的工厂模式,对应代码为:
DAL
using System.Collections.Generic;
using MetalsExchange.IDAL;
using MetalsExchange.DBUtility;
using MetalsExchange.Model;
namespace MetalsExchange.SQLServerDAL
{
public class Employee: IEmployee
{
public IList<CustomerInfo> GetCustomerList()
{
IList<CustomerInfo> list = new List<CustomerInfo>();
return list;
}
}
}
IDAL
using System.Collections.Generic;
using MetalsExchange.Model;
namespace MetalsExchange.IDAL
{
public interface IEmployee
{
IList<CustomerInfo> GetCustomerList();
}
}
BLL
using System.Collections.Generic;
using MetalsExchange.Model;
using MetalsExchange.IDAL;
namespace MetalsExchange.Bll
{
public class EmployeeBll
{
public IEmployee dal = DALFactory.DataAccess.CreateEmployee();
public IList<CustomerInfo> GetCustomerList()
{
return dal.GetCustomerList();
}
}
}
DALFactory
using System.Configuration;
using System.Reflection;
namespace MetalsExchange.DALFactory
{
public class DataAccess
{
private static string path {
get {
string _path= ConfigurationManager.AppSettings["WebDAL"];
if (_path == null)
_path = "MetalsExchange.SQLServerDAL";
return _path;
}
}
public static IDAL.IEmployee CreateEmployee()
{
string className = path + ".Employee";
return (IDAL.IEmployee)Assembly.Load(path).CreateInstance(className);
}
}
}
最开始就卡在这里,一度以为静态方法无法单元测试,后来发现原因是因为ConfigurationManager.AppSettings["WebDAL"]取不到值,因为测试项目里面没有config文档,目前我采用了最简单的方法来解决,最好的方法是将这个方法重新封装
单元测试
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Moq;
using MetalsExchange.IDAL;
using MetalsExchange.Model;
namespace MetalsExchange.Bll.Tests
{
[TestClass()]
public class EmployeeBllTests
{
[TestMethod()]
public void GetCustomerListTest()
{
//arrange
var mock = new Mock<IEmployee>();
mock.Setup(customer => customer.GetCustomerList()).Returns(new List<CustomerInfo>());
EmployeeBll employee = new EmployeeBll();
//art
IList<CustomerInfo> list = employee.GetCustomerList();
mock.Verify();
//assert
Assert.ReferenceEquals(list, mock.Object.GetCustomerList());
}
}
}
工厂类是一个静态类,其实也可以做单元测试
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MetalsExchange.IDAL;
namespace MetalsExchange.DALFactory.Tests
{
[TestClass()]
public class DataAccessTests
{
[TestMethod()]
public void CreateEmployeeTest()
{
IEmployee dal = DataAccess.CreateEmployee();
Assert.IsNotNull(dal);
}
}
}
单元测试之初试
发表于:2017-01-09
作者:网络转载
来源:
 相关文章
单元测试系列之一开篇 单元测试的实践与思考 Python单元测试之道:从入门到精通 单元测试的重要性:编写更安全、更可... 简化Java单元测试数据 Golang单元测试详解:单元测试的基本...- 周排行
- 月排行
- 评论排行
-   单元测试的五个主要准则
-   单元测试:优雅的Spock框架
-   Android开发中如何进行单元测试?
-   对Controller的单元测试
-   单元测试的实践与思考
-   单元测试的重要性:编写更安全、更可...
-   你们单测覆盖率是如何统计的?原理是...
-   单元测试的五个主要准则
-   Android开发中如何进行单元测试?
-   什么是单元测试,和集成测试有什么区别?
-   一次单元测试优化的过程总结
-   黑盒测试方法之组合覆盖法
-   对Controller的单元测试
-   单元测试—DbUnit测试
-   从软件出发,非功能测试思考总结
-   展望2017,谁是网络技术头牌?
-   C/C++单元测试工具Visual Unit 4 介绍
-   国内外最好用的6款Bug跟踪管理系统
-   性能测试指标评估必读
-   百度软件测试方案模板
-   38张史上最全的IT工程师技能图谱
-   C/C++单元测试工具Visual Unit 4 介绍
-   2019年,你会选哪些安全测试工具?
-   十款Web服务器性能压力测试工具
-   软件测试入门指南:周期、模型和文档化
-   记自动化测试成神学习之路
-   软件测试全景图 -最全的思维导图