Skip to content

Instantly share code, notes, and snippets.

@jayhjkwon
Last active September 10, 2020 09:32
Show Gist options
  • Save jayhjkwon/6260978 to your computer and use it in GitHub Desktop.
Save jayhjkwon/6260978 to your computer and use it in GitHub Desktop.
WebAPI2 Unit Testing with IHttpActionResult
// 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);
}
[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"]);
}
@sujinlee
Copy link

test

@richheey
Copy link

test

@richheey
Copy link

test

@SridharPasham
Copy link

Nice. Thanks.

@denisdoci
Copy link

very good thank you

@wovalle
Copy link

wovalle commented Oct 6, 2016

Excelent!

@Lechus
Copy link

Lechus commented Nov 21, 2016

Hi,
Why there is no test for return BadRequest(ModelState); ?

@lokeshkumar03
Copy link

lokeshkumar03 commented Jun 6, 2017

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);
        }
    }

@Barsa71
Copy link

Barsa71 commented Sep 10, 2020

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