我第一次尝试实现测试驱动的开发。我的项目是 dotnet 3.5 中的 ac#。我已经阅读了 C# 中的专业测试驱动开发一书,现在我想测试包含 Windows 服务的项目。我读过最佳实践是所有代码都必须经过测试。以下是我的 Windows 服务实现方法 onStart 和 onStop
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using log4net;
namespace MyUcmaService
{
public partial class MyUcmaService : ServiceBase
{
private Worker _workerObject;
private static MyUcmaService aMyUcmaService;
private Thread _workerThread;
private static ILog _log = LogManager.GetLogger(typeof(MyUcmaService));
public MyUcmaService()
{
InitializeComponent();
aMyUcmaService = this;
}
protected override void OnStart(string[] args)
{
// TODO: inserire qui il codice necessario per avviare il servizio.
//Debugger.Launch();
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
try
{
_workerObject = new Worker();
_workerThread = new Thread(_workerObject.DoWork);
// Start the worker thread.
_workerThread.Start();
}
catch (Exception ex)
{
HandleException(ex);
}
}
protected override void OnStop()
{
// TODO: inserire qui il codice delle procedure di chiusura necessarie per arrestare il servizio.
try
{
_workerObject.RequestStop();
_workerThread.Join();
}
catch (Exception ex)
{
HandleException(ex);
}
}
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleException(e.ExceptionObject as Exception);
}
private static void HandleException(Exception ex)
{
if (ex == null)
return;
_log.Error(ex);
if (aMyUcmaService != null)
{
aMyUcmaService.OnStop();
}
}
}
}
你能告诉我如何在这里实现 tdd 吗?感谢您的回复。