单元测试如下:

  d、UnitTest

 

using System;
using MonitorService;
using MonitorService.Impl;
using Ninject;
using NUnit.Framework;

namespace UnitTestApp
{
    [TestFixture]
    public class MonitorServiceTest_Manual
    {
        public ConfigFileMonitor CurrentFileMonitor
        {
            get;
            set;
        }

        public ILogService LogService
        {
            get;
            set;
        }

        public IEmailService EmailService
        {
            get;
            set;
        }

        [SetUp]
        public void SetUp()
        {
            using (var kernel = new StandardKernel(new ServiceModule()))
            {
                this.CurrentFileMonitor = kernel.Get<ConfigFileMonitor>();
                this.CurrentFileMonitor.LogService = kernel.Get<StubLogService>();
                this.CurrentFileMonitor.EmailService = kernel.Get<MockEmailService>();
            }
        }

        [Test]
        public void FileMonitor_Inject_GetInstance()
        {
            Assert.IsNotNull(CurrentFileMonitor, "ConfigFileMonitor is not initialized");
        }

        [Test]
        public void FileMonitor_LogService_Inject_GetInstance()
        {
            Assert.IsNotNull(CurrentFileMonitor.LogService, "ConfigFileMonitor stub LogService is not initialized");
        }

        [Test]
        public void FileMonitor_EmailService_Inject_GetInstance()
        {
            Assert.IsNotNull(CurrentFileMonitor.EmailService, "ConfigFileMonitor mock EmailService is not initialized");
        }

        [Test]
        public void Analyze_WebServiceThrows_SendEmail()
        {
            CurrentFileMonitor.LogService.ExToThrow = new NotImplementedException("fake exception");
            var shortFileName = "abc.txt";
            CurrentFileMonitor.Analyze(shortFileName);
            Assert.AreEqual(""mailto:jeffwong@cnblogs.com" jeffwong@cnblogs.com ", CurrentFileMonitor.EmailService.To);
            Assert.AreEqual("filename check", CurrentFileMonitor.EmailService.Subject);
            Assert.AreEqual("fake exception", CurrentFileMonitor.EmailService.Body);
            //Assert.AreEqual("fake object", CurrentFileMonitor.EmailService.Body);
        }

        [TearDown]
        public void TearDown()
        {
            this.CurrentFileMonitor = null;
        }

    }
}

  其中,服务调用我们通过Ninject实现解耦,为了简单看到效果,这里没有直接通过构造函数或者属性实现依赖注入。

  通过上面的说明我们确实可以得出这两个伪对象是有明显区别的,某些书洋洋洒洒毫不吝惜笔墨写了许多页,专门介绍这两三个概念的异同,但是看的人依然云里雾里。