I am trying to get a unit test(more of an integration test) written for the GetAwesomeResultsAsXml()
for the following WCF Rest Service.
How do I deal with the WebOperationContext
mocking aspect?
What would be the best approach?
public class AwesomeRestService : AwesomeRestServiceBase, IAwesomeRestService
{
public AwesomeSearchResults<AwesomeProductBase> GetAwesomeResultsAsXml()
{
return GetResults();
}
private static AwesomeSearchResults<AwesomeProductBase> GetResults()
{
var searchContext = AwesomeSearchContext
.Parse(WebOperationContext.Current);
..............
..............
..............
}
}
[ServiceContract]
public interface IAwesomeRestService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/search/xml")]
AwesomeQueryResults<AwesomeProductBase> GetAwesomeResultsAsXml();
}
public class AwesomeSearchContext
{
................
................
................
public static AwesomeSearchContext Parse
(WebOperationContext operationContext)
{
return WebOperationContext.Current != null ? new
AwesomeSearchContext(operationContext.IncomingRequest.UriTemplateMatch.QueryParameters) : null;
}
}
I met the same problem. I want to unit test a WCF service function (for IOauth2 interface as below example) without any IIS. This is the code snippet for the preparation.
// Prepare WebOperationContext
var factory = new ChannelFactory<IOauth2>(
new WebHttpBinding(),
new EndpointAddress("http://localhost:80"));
OperationContext.Current = new OperationContext(factory.CreateChannel() as IContextChannel);
Debug.Assert(WebOperationContext.Current != null);
I followed Sanjay's answer, and try the MS fake framework,
First of all, you have to open "Solution Explorer > your test project > Reference"
=> right-click the "System.ServiceModel.Web"
=> press "add Fakes Assembly"
using Microsoft.QualityTools.Testing.Fakes;
using System.ServiceModel.Web.Fakes;
using (ShimsContext.Create())
{
var response = new ShimOutgoingWebResponseContext();
var request = new ShimIncomingWebRequestContext();
var ctx_hd = new WebHeaderCollection();
ctx_hd.Add("myCustomHeader", "XXXX");
request.HeadersGet = () => ctx_hd;
var ctx = new ShimWebOperationContext
{
OutgoingResponseGet = () => response,
IncomingRequestGet = () => request
};
ShimWebOperationContext.CurrentGet = () => ctx;
//Test your code here...
}
and now you can get the WebOperationContext.Current.IncomingRequest.Headers["myCustomHeader"] in your WCF service code now.
More about MS Fakes framework on MSDN: https://msdn.microsoft.com/en-us/library/hh549176.aspx
A common approach to this is mocking tool like moq (https://code.google.com/p/moq/) or rhinomocks.
As they don't allow you to mock static members you would need to wrap the call to webcontext.current. Here is an example of wrapping a static mmember and testing with moq: Mock static property with moq
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With