Skip to content

CtcApi.Testing

Shawn (work acct) edited this page Jun 8, 2013 · 2 revisions

The Testing namespace provides classes and methods to ease the creation of unit tests.

FakeHttpHelper (testing code that makes HTTP calls)

The classes for HTTP communication in the .NET Framework provide a mechanism for injecting mock Request and Response objects that can be used to provide canned functionality. This allows you to test your code without actually connecting to a web server - which in turn makes your unit tests more portable and much faster. To simplify this capability, the CtcApi provides the FakeHttpHelper class.

Code for the FakeHttpHelper class is based on this article: How to Mock HttpWebRequest when Unit Testing. It is intended to be used in a standard Visual Studio unit testing project and requires the Microsoft.VisualStudio.TestTools.UnitTesting namespace.

Examples

The following example will register mock HTTP objects to handle URLs that begin with test://.

 // initialize the helper with the URL schema and text of the response you want it to provide.
 FakeHttpHelper http = new FakeHttpHelper("test", "This is the body of the HTTP response.");

 // identify any headers you expect the code being tested to add
 http.ExpectedHeaders.Add("Authorization", String.Concat("Bearer ", "AUTHENTICATION TOKEN GOES HERE"));

 // *******************************
 // Run the code to be tested here
 // *******************************

 // verify code being tested has set the expected HTTP headers
 http.AssertExpectedHeaders();

 // verify expected Request method has been set
 http.AssertRequestMethod("POST");

 // Perform any additional Assertions required to validate the test