单元测试WCF行为扩展 [英] Unit Testing a WCF Behavior Extension
问题描述
这是推动我疯了。我有我们用来改变请求的内容响应,如果有错误的扩展。基本上,如果一切正常,它被序列化到JSON作为正常的,但如果我们得到一个未处理的异常,我们序列基于一个不同的对象。
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.
我们需要解决这个单元测试但我无法弄清楚如何写他们。的手段来验证它的工作围绕的StatusCode,故障信息实例和响应的ContentType的旋转。
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.
要修改响应回去,我利用WebOperationContext静态类。我见过嘲笑他的例子,但它似乎像我有硬编码特殊的逻辑,然后将开始渗透到实际的代码,这是我不想要的。
To modify the Response going back, I utilize the WebOperationContext static class. 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.
一些最好的方法有哪些单元测试WCF行为扩展?
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.
我所做的就是让我的行为只是申请一个 IParameterInspector
和 IClientMessageInspector
(在我的情况,我需要两个),并把我所有的逻辑,无论是在 AfterCall
或 BeforeCall
,或任何一个你所需要的,这取决于检查类型。
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.
这样我可以测试所有我关心的逻辑。
的实际WCF行为仍未经检验的,但所有这确实只是添加两个督察,别无其他。
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行为扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!