-
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
4 changed files
with
90 additions
and
7 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
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,73 @@ | ||
namespace ToDoList.Test.UnitTests; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NSubstitute; | ||
using NSubstitute.ExceptionExtensions; | ||
using ToDoList.Domain.Models; | ||
using ToDoList.Persistence.Repositories; | ||
using ToDoList.WebApi.Controllers; | ||
|
||
public class GetUnitTests | ||
{ | ||
[Fact] | ||
public void Get_ReadWhenSomeItemAvailable_ReturnsOk() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
repositoryMock.ReadAll().Returns( | ||
[ | ||
new ToDoItem{ | ||
Name = "testName", | ||
Description = "testDescription", | ||
IsCompleted = false | ||
} | ||
] | ||
); | ||
|
||
// Act | ||
var result = controller.Read(); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<OkObjectResult>(resultResult); | ||
repositoryMock.Received(1).ReadAll(); | ||
} | ||
|
||
[Fact] | ||
public void Get_ReadWhenNoItemAvailable_ReturnsNotFound() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
// repositoryMock.ReadAll().ReturnsNull(); | ||
repositoryMock.ReadAll().Returns(null as IEnumerable<ToDoItem>); | ||
|
||
// Act | ||
var result = controller.Read(); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<NotFoundResult>(resultResult); | ||
repositoryMock.Received(1).ReadAll(); | ||
} | ||
|
||
[Fact] | ||
public void Get_ReadUnhandledException_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
repositoryMock.ReadAll().Throws(new Exception()); | ||
|
||
// Act | ||
var result = controller.Read(); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(resultResult); | ||
repositoryMock.Received(1).ReadAll(); | ||
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