c# - Unit testing an HttpApplication -
I have a class from HTTP applications that adds some extra features. I am at the point where I need the unit to test these facilities, which means that I have to be able to create a new installation of HTTPEpiation, to duplicate a request and retrieve the feedback object.
How OK do I run the unit to test a HTTPPoint object? I am using Moq this time, but I do not know how to set up the required counterfeit stuff. Unfortunately, this is not particularly easy to do, because the http application itself is very easy to joke.
Does not lend in; There is no interface to mock against it and most of the methods have not been marked as virtual.
Recently, I had a similar problem with HTTP and HTT Webbashings. In the end, the solution I went to was to create a straightforward "pass-through" cover for those methods I wanted to use:
public class HttpWebRequestWrapper: IHttpWebRequestWrapper { Private HttpWebRequest httpWebRequest; Public HttpWebRequestWrapper (vertical url) {this.httpWebRequest = (HttpWebRequest) HttpWebRequest.Create (url); } Public Stream GetRequestStream () {Return it HttpWebRequest.GetRequestStream (); } Public IHttpWebResponseWrapper GetResponse () {new HttpWebResponseWrapper (this .httpWebRequest.GetResponse ()); } Receive public Int64 content {this return.HttpWebRequest.ContentLength; } Set {this.httpWebRequest.ContentLength = value; }} Get the public string method {get it back. HpppWebRequest.method; } Set {this.httpWebRequest.method = value; }} Public String ContentType {get {return this.httpWebRequest.ContentType; } Set {this.httpWebRequest.ContentType = value; }}}
etc., etc.
This is a fake against my own cover interface. Not necessarily the most beautiful thing in the world, but there is a very useful way of making fun of some of the "fake" parts of the structure.
Before you depart, and although doing so, it is worth to review what you have got and if you have a better view of the tests, then you will avoid wrapping the classes.
In the case of HttpWebRequest, HttpApplication et al, there is often not IMHO.
To set this wrapper in duplicate (using the above example my HTTPView), you then do this with MUK:
var mockWebRequest = New mock & lt; IHttpWebRequestWrapper & gt; (); MockWebRequest.SetupSet & lt; String & gt; (C = & gt; c.method = "POST"). Verifiable (); MockWebRequest.SetupSet & lt; String & gt; (C = & gt; c.ContentType = "app / x-www-form-urxcode"). Verification (); MockWebRequest.SetupSet & lt; Int & gt; (C => c.contentLength = 0). Verified ();
Comments
Post a Comment