Last active
December 15, 2015 16:39
-
-
Save mauricedb/5290642 to your computer and use it in GitHub Desktop.
Unit testing a WebAPI controller using the HttpClient and server config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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