开发者

Testing WCF services without starting them

I want to test my WCF classes (Integration testing) but I don't want to start them as WCF services.

Everything works well but in some cases I need to read IncomingMessageHeaders from OperationContext. I wonder if this can be done?

What I've learned to far is that I can create OperationContext and use it. But I'd need to cr开发者_JAVA技巧eate it via ChannelFactory which requires Binding (and then Endpoint). What I need (probably) is somehow make a local binding to my implementation class. Is this possible?


The best way to handle this is to use DI/IoC to abstract OperationContext out to an interface and then use a mocking framework like Moq or Rhino to implement that interface. When you test, your mocking framework will provide an instance of that new class in place of OperationContext. That completely removes your dependency on something external during testing.


The alternative is to make each WCF method a one-liner, calling a business logic layer, which you will be able to unit test without having to worry about WCF implementation details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜