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) - 使用桩对象接触依赖的更多相关文章

  1. [Test] 单元测试艺术(2) 打破依赖,使用模拟对象,桩对象,隔离框架

    在上节中,完成了第一个单元测试,研究了各种特性,在本节,将介绍一些更实际的例子.SUT依赖于一个不可操控的对象,最常见的例子是文件系统,线程,内存和时间等. 本系列将分成3节: 单元测试基础知识 打破 ...

  2. [Test] 单元测试艺术(1) 基础知识

    单元测试不是软件开发的新概念,在1970年就一直存在,屡屡被证明是最理想的方法之一. 本系列将分成3节: 单元测试基础知识 打破依赖,使用模拟对象,桩对象,测试框架 创建优秀的单元测试 本节索引: 单 ...

  3. spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象

    相关 知识 >>> 相关 练习 >>> 实现要求: 在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXm ...

  4. .NET单元测试艺术(1) - 单元测试的基本知识

    List 1.1 一个要测试的SimpleParser类 using System; namespace AOUT.CH1.Examples { public class SimpleParser { ...

  5. .NET单元测试艺术(2) - 第一个单元测试

    List 2.1 使用[SetUp]和[TearDown]特性 using System; using System.Collections.Generic; using System.Linq; u ...

  6. Spring 中初始化一个Bean对象时依赖其他Bean对象空指针异常

    1. Bean依赖关系 一个配置类的Bean,一个实例Bean: 实例Bean初始化时需要依赖配置类的Bean: 1.1 配置类Bean @ConfigurationProperties(prefix ...

  7. 谈谈php对象的依赖

    通过构造函数的方法 <?php //定义一个类,后面的类依赖这个类里面的方法 class play { public function playing() { echo "I can ...

  8. Mvc controller单元测试 Mock Url对象

    被测试Action 包含有Url对象的代码: data = new data { title = ds.Name, icon = "folder", attr = new { id ...

  9. 基于spring与mockito单元测试Mock对象注入

    转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...

随机推荐

  1. Holding Bin-Laden Captive!(杭电1085)(母函数)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  2. log(n)在第一时间,以确定该阵列i小号码

    简介参考算法9.2 int partition(int *a,int p,int r){ int x=a[r]; int i=p-1; for(int j=p;j<=r-1;j++){ if(a ...

  3. error C2248: “CObject::operator =”: 不可访问 private 员(于“CObject”类声明)

    MFC如果编码错误: 演出:error C2248: "CObject::operator =": 不可访问 private 员(于"CObject"类声明) ...

  4. 解决android3.0版本号以上应用接收不到开机广播问题

    如今是2014-07-16 下午15:27. 好久没写过东西,突然间灵感喷发想写点东西(事实上是刚刚弄好了一个棘手的问题,自豪中..呵呵呵呵 我牛掰).废话不多说,进入正题. 不知道你们又没有碰到这问 ...

  5. SharePoint使用BCS开发你第一个应用程序(一)

    SharePoint使用BCS开发你第一个应用程序(一)         本系列教你使用BCS(Business Connectivity Service)创建OBA(Office business ...

  6. 模板引擎mustache.js

    Javascript模板引擎mustache.js详解   阅读目录 1. 从一个简单真实的需求讲起 2. mustache的用法 3. mustache的思想 4. {{prop}}标签 5. {{ ...

  7. 查看linux系统版本号命令

    一.查看内核版本号命令: 1) [root@SOR_SYS ~]# cat /proc/version Linux version 2.6.18-238.el5 (mockbuild@x86-012. ...

  8. 《炉石传说》建筑设计欣赏(6):卡&amp;在执行数据时,组织能力

    上一篇文章我们看到了<炉石传说>核心存储卡的数据,今天,我们不断探索卡&身手. 基本的类 通过之前的分析,卡牌&技能涉及到几个类体系:Entity.Actor.Card.S ...

  9. lua-TestMore(转)

    http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...

  10. 让c#的exe只要被修改就无法运行,支持混淆和数字证书

    原文:让c#的exe只要被修改就无法运行,支持混淆和数字证书 首先用sdk的sn工具或者makecert工具生成公钥和密钥,推荐makecert,做自己的证书,我做了一个受信任的根证书放在受信任的根证 ...