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. DSL简介(转)

    DSL编程:有人将DSL编程称之为声明式(Declarative)编程.DSL是在模型之上建立的一种更加灵活的对 模型化的理解和使用方式.语义模型是DSL的核心.内部DSL:用通用语言的语法表示DSL ...

  2. SQL Server :理解BCM页

    原文:SQL Server :理解BCM页 今天我们来讨论下批量更改映射(Bulk Changed Map:BCM)页,还有大容量日志恢复模式( bulk logged recovery model  ...

  3. Windows Phone开发(7):当好总舵主

    原文:Windows Phone开发(7):当好总舵主 吹完了页面有关的话题,今天我们来聊一下页面之间是如何导航的,在更多情况下,我们的应用程序不会只有一个页面的,应该会有N个,就像我们做桌面应 用开 ...

  4. C语言程序代写(QQ:928900200)

    1.学生成绩统计 要求描述: 用结构数组实现学生信息的统计功能. struct student { long no; /*学号*/ char name[10]; /*姓名*/ char sex; /* ...

  5. 索尼 LT26I刷机包 X.I.D 增加官方风格 GF A3.9.4 各方面完美

    ROM介 FX_GF_A系列是具有官方风格的.稳定的.流畅的.省电的.新功能体验的.最悦耳音效体验的ROM. FX_GF_A更新日志 ☆ GF_3.9.4 更新信息 ☆ 更新播放器 ☆ 更新adsp数 ...

  6. javascript---在自由落体实现

    实现一些简单的物业自由落体需要理解: clientHeight:浏览器客户机的整体高度 offsetHeight:物(实例div)高低 offsetTop:从对象client最顶层的距离 简单demo ...

  7. 《Linux内核设计与实现》的地址空间读书笔记的过程

    1.核心区域进程包括各种内存对象 种内存对象,比方: 1.可运行文件代码能够包括各种内存映射,称为代码段(text section). 2.可运行文件的已初始化全局变量的内存映射,称为数据段(data ...

  8. 将jar要么aar公布到到网mvn 在(使用github作为仓库), 通过gradle dependency 信息集成

    使用Android Studio用户开发,都希望通过maven该方式整合远程仓库jar.aar文件.但如何将这些文件发布它? 发人员都会将jar文件公布到sonatype上,以提供给其它开发人员集成, ...

  9. Google Protocol Buffers和java字符串处理控制

    大多数的操作码被从夜晚复制.懒得敲. 直接在源代码和测试结果如下. serabuffer.proto档.使用下面的命令来生成java代码. protoc -I=./ --java_out=./ ser ...

  10. Windows 7下一个:该应用程序不能正常启动(0xc0150002)

             在新系统中正确安装QQ2010无法执行,同一时候安装的TM2009也无法执行. 相同显示为"应用程序无法正常启动(0xc0150002). 请单击"确定" ...