-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
namespace ToDoList.Test.UnitTests; | ||
|
||
using NSubstitute; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ToDoList.WebApi.Controllers; | ||
using ToDoList.Persistence.Repositories; | ||
using ToDoList.Domain.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute.ExceptionExtensions; | ||
using NSubstitute.ReturnsExtensions; | ||
|
||
public class GetByIdUnitTests | ||
{ | ||
[Fact] | ||
public void Get_ReadByIdWhenSomeItemAvailable_ReturnsOk() | ||
Check warning on line 15 in ToDoList/tests/ToDoList.Test/UnitTests/GetByIdTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var someId = 1; | ||
repositoryMock.ReadById(someId).Returns(new ToDoItem { Name = "testItem", Description = "testDescription", IsCompleted = false }); | ||
|
||
// Act | ||
var result = controller.ReadById(someId); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<OkObjectResult>(resultResult); | ||
repositoryMock.Received(1).ReadById(someId); | ||
} | ||
|
||
[Fact] | ||
public void Get_ReadByIdWhenItemIsNull_ReturnsNotFound() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var someId = 1; | ||
repositoryMock.ReadById(someId).ReturnsNull(); | ||
|
||
// Act | ||
var result = controller.ReadById(someId); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<NotFoundResult>(resultResult); | ||
repositoryMock.Received(1).ReadById(someId); | ||
} | ||
|
||
[Fact] | ||
public void Get_ReadByIdUnhandledException_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var someId = 1; | ||
repositoryMock.ReadById(someId).Throws(new Exception()); | ||
|
||
// Act | ||
var result = controller.ReadById(someId); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(resultResult); | ||
repositoryMock.Received(1).ReadById(someId); | ||
Assert.Equivalent(new StatusCodeResult(StatusCodes.Status500InternalServerError), resultResult); | ||
} | ||
|
||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
namespace ToDoList.Test.UnitTests; | ||
|
||
using NSubstitute; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ToDoList.Domain.DTOs; | ||
using ToDoList.WebApi.Controllers; | ||
using ToDoList.Persistence.Repositories; | ||
using ToDoList.Domain.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute.ReturnsExtensions; | ||
|
||
public class PutUnitTests | ||
{ | ||
[Fact] | ||
public void Put_UpdateByIdWhenItemUpdated_ReturnsNoContent() | ||
Check warning on line 15 in ToDoList/tests/ToDoList.Test/UnitTests/PutTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var request = new ToDoItemUpdateRequestDto( | ||
Name: "Jmeno", | ||
Description: "Popis", | ||
IsCompleted: false | ||
); | ||
var someId = 1; | ||
var readToDoItem = new ToDoItem { Name = "Jmeno", Description = "Popis", IsCompleted = false, ToDoItemId = someId }; | ||
repositoryMock.ReadById(someId).Returns(readToDoItem); | ||
|
||
// Act | ||
var result = controller.UpdateById(someId, request); | ||
|
||
// Assert | ||
Assert.IsType<NoContentResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
repositoryMock.Received(1).Update(Arg.Any<ToDoItem>()); | ||
} | ||
|
||
[Fact] | ||
public void Put_UpdateByIdWhenIdNotFound_ReturnsNotFound() | ||
Check warning on line 39 in ToDoList/tests/ToDoList.Test/UnitTests/PutTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var request = new ToDoItemUpdateRequestDto( | ||
Name: "Jmeno", | ||
Description: "Popis", | ||
IsCompleted: false | ||
); | ||
repositoryMock.ReadById(Arg.Any<int>()).ReturnsNull(); | ||
var someId = 1; | ||
|
||
// Act | ||
var result = controller.UpdateById(someId, request); | ||
|
||
// Assert | ||
Assert.IsType<NotFoundResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
} | ||
|
||
[Fact] | ||
public void Put_UpdateByIdUnhandledException_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var request = new ToDoItemUpdateRequestDto( | ||
Name: "Jmeno", | ||
Description: "Popis", | ||
IsCompleted: false | ||
); | ||
var someId = 1; | ||
var readToDoItem = new ToDoItem { Name = "Jmeno", Description = "Popis", IsCompleted = false, ToDoItemId = someId }; | ||
|
||
repositoryMock.ReadById(Arg.Any<int>()).Returns(readToDoItem); | ||
repositoryMock.When(r => r.Update(Arg.Any<ToDoItem>())).Do(r => throw new Exception()); | ||
|
||
// Act | ||
var result = controller.UpdateById(someId, request); | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(result); | ||
Assert.Equivalent(new StatusCodeResult(StatusCodes.Status500InternalServerError), result); | ||
} | ||
} |