.NET单元测试艺术(3) - 使用桩对象接触依赖
List 3.1 抽取一个设计文件系统的类,并调用它
[Test]
public bool IsValidLogFileName(string fileName)
{
FileExtensionManager mgr = new FileExtensionManager();
return mgr.IsValid(fileName);
}
class FileExtensionManager
{
public bool IsValid(string fileName)
{
// Read file
return true;
}
}
List 3.2 从一个已知类中抽取一个接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AOUT.LogAn
{
public interface IExtensionManager
{
bool IsValid(string fileName);
}
public class FileExtensionManager : IExtensionManager
{
public bool IsValid(string fileName)
{
// ...
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace AOUT.LogAn
{
public class LogAnalyzer
{
// File to be tested
public bool IsValidLogFileName(string fileName)
{
IExtensionManager mgr = new FileExtensionManager();
return mgr.IsValid(fileName);
}
}
}
List 3.3 永远返回true的简单桩对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AOUT.LogAn
{
public class StubExtensionManager : IExtensionManager
{
public bool IsValid(string fileName)
{
return true;
}
}
}
List 3.4 通过构造函数注入桩对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace AOUT.LogAn
{
public class LogAnalyzer
{
private IExtensionManager _manager;
public LogAnalyzer()
{
_manager = new FileExtensionManager();
}
public LogAnalyzer(IExtensionManager mgr)
{
_manager = mgr;
}
// Method to be tested
public bool IsValidLogFileName(string fileName)
{
return _manager.IsValid(fileName);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace AOUT.LogAn.Tests
{
[TestFixture]
public class LogAnalyzerTests
{
[Test]
public void IsValidFileName_NameShorterThan6CharsButSupportedExtension_ReturnsFalse()
{
StubExtensionManager myFakeManager = new StubExtensionManager();
myFakeManager.ShouldExtensionBeValid = true;
LogAnalyzer log = new LogAnalyzer(myFakeManager);
bool result = log.IsValidLogFileName("short.ext");
Assert.IsFalse(result, "File name with less than 5 chars should have failed the method," +
"even if the extension is supported");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AOUT.LogAn
{
public class StubExtensionManager : IExtensionManager
{
private bool _shouldExtensionBeValid;
public bool ShouldExtensionBeValid
{
get { return _shouldExtensionBeValid; }
set { _shouldExtensionBeValid = value; }
}
public bool IsValid(string fileName)
{
return ShouldExtensionBeValid;
}
}
}
.NET单元测试艺术(3) - 使用桩对象接触依赖的更多相关文章
- [Test] 单元测试艺术(2) 打破依赖,使用模拟对象,桩对象,隔离框架
在上节中,完成了第一个单元测试,研究了各种特性,在本节,将介绍一些更实际的例子.SUT依赖于一个不可操控的对象,最常见的例子是文件系统,线程,内存和时间等. 本系列将分成3节: 单元测试基础知识 打破 ...
- [Test] 单元测试艺术(1) 基础知识
单元测试不是软件开发的新概念,在1970年就一直存在,屡屡被证明是最理想的方法之一. 本系列将分成3节: 单元测试基础知识 打破依赖,使用模拟对象,桩对象,测试框架 创建优秀的单元测试 本节索引: 单 ...
- spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象
相关 知识 >>> 相关 练习 >>> 实现要求: 在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXm ...
- .NET单元测试艺术(1) - 单元测试的基本知识
List 1.1 一个要测试的SimpleParser类 using System; namespace AOUT.CH1.Examples { public class SimpleParser { ...
- .NET单元测试艺术(2) - 第一个单元测试
List 2.1 使用[SetUp]和[TearDown]特性 using System; using System.Collections.Generic; using System.Linq; u ...
- Spring 中初始化一个Bean对象时依赖其他Bean对象空指针异常
1. Bean依赖关系 一个配置类的Bean,一个实例Bean: 实例Bean初始化时需要依赖配置类的Bean: 1.1 配置类Bean @ConfigurationProperties(prefix ...
- 谈谈php对象的依赖
通过构造函数的方法 <?php //定义一个类,后面的类依赖这个类里面的方法 class play { public function playing() { echo "I can ...
- Mvc controller单元测试 Mock Url对象
被测试Action 包含有Url对象的代码: data = new data { title = ds.Name, icon = "folder", attr = new { id ...
- 基于spring与mockito单元测试Mock对象注入
转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...
随机推荐
- 树上第k小,可持久化线段树+倍增lca
给定一颗树,树的每个结点都有权值, 有q个询问,每个询问是 u v k ,表示u到v路径上第k小的权值是多少. 每个结点所表示的线段树,是父亲结点的线段树添加该结点的权值之后形成的新的线段树 c[ro ...
- TCP header
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vzc2VyNDM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- (hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)
题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HiPAC高性能规则匹配算法之查找过程
收到一封邮件,有位朋友认为我误解了nf-HiPAC.如此的一个高性能算法怎能被什么传统的hash,tree之类的胁迫.是啊.HiPAC是一个非常猛的算法.文档也比較少,这就更加添加了其神奇感,可是这决 ...
- key 串口
/******************************************************************** 函数功能:往串口发送一字节数据(可通过超级终端或者串口调试助 ...
- 第3周 区_SQL Server中管理空间的基本单位
原文:第3周 区_SQL Server中管理空间的基本单位 哇哦,SQL Server性能调优培训已经进入第3周了!同时你已经对SQL Server内核运行机制有了很好的认识.今天我会讲下SQL Se ...
- SQLServer 网络协议(一)
SQLserver现在主要的3种协议:Shared Memory.TCP/IP 和 Named Pipe SharedMemory: Shared Memory最快最简单的协议,使用SharedMem ...
- Linux 下卸载MySQL 5
对于在Linux下通过rpm方式的mysql,我们能够通过移除这些rpm包以及删除项目的文件夹来达到卸载的目的.本文演示了在SUSE Linux 10下下载MySQL 5.5.37.详细见下文. 1. ...
- WinForm、wpf、silverlight三者关系
最近在学C#.NET,基本语法学习的差不多了,接下来准备学习图形界面设计部分.但是我目前对于.NET的WinForm.wpf.silverlight这三者的关系弄的不是很清楚,一般书中很少介绍wpf和 ...
- 【原创】leetCodeOj --- Jump Game II 解题报告
原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...