简体   繁体   English

WCF行为扩展的单元测试

[英]Unit Testing a WCF Behavior Extension

This is driving me crazy. 这真让我抓狂。 I have an extension which we use to alter the Content Response of a request if there is an error. 我有一个扩展名,如果有错误,我们可以使用它来更改请求的内容响应。 Basically, if everything works it gets serialized to JSON as normal, but if we get an unhandled exception, we serialize based on a different object. 基本上,如果一切正常,它将像往常一样序列化为JSON,但是如果我们遇到未处理的异常,则会基于不同的对象进行序列化。

We need unit tests around this but I cannot figure out how to write them. 我们需要围绕此进行单元测试,但我无法弄清楚如何编写它们。 The means to validate that it worked revolves around the StatusCode, the fault Message instance, and the ContentType of the response. 验证其是否有效的方法围绕着StatusCode,故障Message实例和响应的ContentType。

To modify the Response going back, I utilize the WebOperationContext static class. 为了修改回溯的Response,我利用了WebOperationContext静态类。 I have seen examples of mocking this, but it seems like I have to hard code special logic that would then begin to percolate into the actual code, which I don't want. 我已经看到了模拟此示例的示例,但是似乎我不得不对特殊逻辑进行硬编码,然后这些特殊逻辑将开始渗入实际的代码中,这是我所不希望的。

What are some of the best ways to unit test a WCF behavior extension? 对WCF行为扩展进行单元测试的最佳方法是什么?

I had a similar situation, and was unable to mock WCF (at least using Moq), mainly because most classes are either sealed or had internal constructors. 我也有类似的情况,并且无法模拟WCF(至少使用Moq),这主要是因为大多数类都是密封的或具有内部构造函数。

What I did was to have my behavior just apply an IParameterInspector and an IClientMessageInspector (in my case I needed both) and put all my logic in either the AfterCall or BeforeCall , or whichever one you need, depending on the inspector type. 我要做的就是让我的行为仅应用IParameterInspectorIClientMessageInspector (在我的情况下,我两者都需要),然后将所有逻辑放到AfterCallBeforeCall ,或者根据检查器的类型放到所需的任何一个中。

That way I could test all the logic I cared about. 这样,我可以测试自己关心的所有逻辑。 The actual WCF Behavior remained untested, but all it really did was just add the two inspectors and nothing else. 实际的WCF行为仍未经测试,但实际上所做的只是添加两个检查器,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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