Last active
September 10, 2020 09:32
-
-
Save jayhjkwon/6260978 to your computer and use it in GitHub Desktop.
WebAPI2 Unit Testing with IHttpActionResult
This file contains hidden or 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
// GET api/books | |
public IHttpActionResult Get() | |
{ | |
return Ok(_repository.GetBooks().AsEnumerable()); | |
} | |
// GET api/books/1 | |
public IHttpActionResult Get(int id) | |
{ | |
var book = _repository.GetBook(id); | |
if (book == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(book); | |
} | |
// PUT api/books/1 | |
public IHttpActionResult Put(int id, Book book) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
return Ok(_repository.UpdateBook(book)); | |
} | |
// POST api/books | |
public IHttpActionResult Post(Book book) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var newBook = _repository.AddBook(book); | |
return CreatedAtRoute("DefaultApi", new { newBook.Id }, newBook); | |
} | |
This file contains hidden or 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 GetShouldReturnAllBooks() | |
{ | |
var controller = new BooksController(); | |
var actionResult = controller.Get(); | |
var response = actionResult as OkNegotiatedContentResult<IEnumerable<Book>>; | |
Assert.IsNotNull(response); | |
var books = response.Content; | |
Assert.AreEqual(1, books.Count()); | |
} | |
[TestMethod] | |
public void GetWithIdItShouldReturnThatBook() | |
{ | |
var controller = new BooksController(); | |
var actionResult = controller.Get(1); | |
var response = actionResult as OkNegotiatedContentResult<Book>; | |
Assert.IsNotNull(response); | |
Assert.AreEqual(1, response.Content.Id); | |
} | |
[TestMethod] | |
public void PutShouldUpdateBook() | |
{ | |
var controller = new BooksController(); | |
var book = new Book { Id = 1, Title = "asp.net webapi2", Author = "scott" }; | |
var actionResult = controller.Put(book.Id, book); | |
var response = actionResult as OkNegotiatedContentResult<Book>; | |
Assert.IsNotNull(response); | |
var newBook = response.Content; | |
Assert.AreEqual(1, newBook.Id); | |
Assert.AreEqual("asp.net webapi2", newBook.Title); | |
Assert.AreEqual("scott", newBook.Author); | |
} | |
[TestMethod] | |
public void PostShouldAddBook() | |
{ | |
var controller = new BooksController(); | |
var actionResult = controller.Post(new Book | |
{ | |
Title = "asp.net webapi2", | |
Author = "scott" | |
}); | |
var response = actionResult as CreatedAtRouteNegotiatedContentResult<Book>; | |
Assert.IsNotNull(response); | |
Assert.AreEqual("DefaultApi", response.RouteName); | |
Assert.AreEqual(response.Content.Id, response.RouteValues["Id"]); | |
} |
test
test
Nice. Thanks.
very good thank you
Excelent!
Hi,
Why there is no test for return BadRequest(ModelState); ?
Here is the code to unit test BadRequest
//Controller
public IHttpActionResult GetTeamMembersForCustomer(Guid custid)
{
try
{
if (custid == Guid.Empty)
{
return BadRequest("CustomerId is null");
}
return Ok(_customerTeamMemberService.GetTeamMembersForCustomer(custid));
}
catch (Exception ex)
{
throw ex;
}
}
// Unit test Methods
public void Get_CustomerTeamMember_ByNonExistingId_empty()
{
try
{
// Act
IHttpActionResult result = _customersTeamMemberController.GetTeamMembersForCustomer(Guid.Empty);
var conNegResult = result as BadRequestErrorMessageResult;
//Assert
Assert.IsTrue(true);
Assert.IsNotNull(conNegResult.Message);
Assert.AreEqual(conNegResult.Message, "CustomerId is null");
}
catch (Exception ex)
{
Assert.IsFalse(true,ex.Message);
}
}
I am getting error like "The type APIController is defined in an assembly that is not referenced.Please help me in that
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test