How can I unit test a Windows Service?

前端 未结 7 1664
青春惊慌失措
青春惊慌失措 2020-12-08 13:03

.NET Framework: 2.0 Preferred Language: C#

I am new to TDD (Test Driven Development).

First of all, is it even possible to unit test Windows Service?

相关标签:
7条回答
  • 2020-12-08 13:45

    Designing for test is a good strategy, as many of the answers point out by recommending that your OnStart and OnStop methods stay very thin by delegating to domain objects.

    However, if your tests do need to execute the service methods for some reason, you can use code like this to call them from within a test method (calling OnStart in this example):

    serviceInstance.GetType().InvokeMember("OnStart", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, serviceInstance, new object[] {new string[] {}});
    
    0 讨论(0)
提交回复
热议问题