.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;
        }
    }
}
上一篇:Ubuntu下将Sublime Text设置为默认编辑器


下一篇:javascript-装饰者模式