Skip to content

Instantly share code, notes, and snippets.

@mauricedb
Last active December 15, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricedb/5290642 to your computer and use it in GitHub Desktop.
Save mauricedb/5290642 to your computer and use it in GitHub Desktop.
Unit testing a WebAPI controller using the HttpClient and server config
[TestMethod]
public void PutViaServer()
{
// Arrange
var config = new HttpConfiguration();
WebApiConfig.Register(config);
var server = new HttpServer(config);
var client = new HttpClient(server);
var book = new Book();
book.Title = "The REST book";
book.Author = "Maurice";
// Act
var response = client.PutAsJsonAsync("http://localhost/api/books/1", book);
//// Assert
Assert.AreEqual(HttpStatusCode.OK, response.Result.StatusCode);
var newBook = response.Result.Content.ReadAsAsync<Book>().Result;
Assert.AreEqual(book.Id, newBook.Id);
Assert.AreEqual(book.Title, newBook.Title);
Assert.AreEqual(book.Author, newBook.Author);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment