在 ServiceStack 中测试插件 [英] Testing plugins in ServiceStack
问题描述
我正在查看 ServiceStack 中单元测试的源代码TestHostBase.cs - 并让它与我自己的项目一起运行.在没有实际启动侦听器的情况下进行单元测试时,我将如何启用我的插件?例如,我想测试我的 Web 服务是否正确验证,因此我将以下代码添加到 TestHostBase.cs 中的配置.
Im looking at the source code for unit testing in ServiceStackTestHostBase.cs - and got it running with my own project. How would i go around enabling my plugins when unit testing without actually starting a listener? For examples i would like to test that my web service validates correctly, so i add the following code to Configure in TestHostBase.cs.
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(CustomerModelValidator).Assembly);
但是当使用违反验证规则的 Customer 对象调用 base.send 时,不会进行验证.(注意:当我使用侦听器启动我的网络服务时,验证工作正常)
But when calling base.send with a Customer object which violates a validation rule no validation are done. (Note: Validation works allright when i start my webservice with a listener)
推荐答案
因为插件可以改变 AppHost 的任何部分(即添加路由、内容类型、请求/响应过滤器、CatchAll 处理程序等).他们无法在不启动 HttpListener(或 ASP.NET 主机)的情况下测试其与您的服务的集成.
Since Plugins can mutate any part of an AppHost (i.e. Add Routes, ContentTypes, Request/Response Filters, CatchAll Handlers, etc). They're not possible to test its integration with your services without starting a HttpListener (or ASP.NET host).
当然,如果您的插件是可测试的,您可以单独对其进行单元测试,与您的服务分开.
Of course if your Plugin is testable, you can unit test it in isolation, separate from your services.
这篇关于在 ServiceStack 中测试插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!