简体   繁体   中英

Unit testing with a mock HttpListener

I should say that I'm fairly new in the mock-framework world of C#. I'm building a small service that listens to incoming HTTP requests using a System.Net.HttpListener and would like to unit test my implementation by injecting a mock instance of that class. So I tried using Moq , but that of course requires that the mock target is an interface so that it can provide a stub implementation. Then I tried using Microsoft Fakes, but it won't let me generate a StubHttpListener . So is there any other way to mock the behaviour of System.Net.HttpListener ?

写一个包装HttpListener的类并从中提取一个接口,然后可以模拟/存根包装器

  1. Create custom interface contains used by service methods of HttpListener
  2. Create own class derived from HttpListener and implements interface from p.1 using HttpListener methods calls
  3. Pull interface from p.1 in service class (using IoC or other mechanics) and map dependency injection to custom class from p.2
  4. Mock custom interface from p.1 to unit test the service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM