Skip to content

Commit

Permalink
Tests done according to Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Hijtec committed Nov 12, 2024
1 parent 2918750 commit 9b87f8a
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 11 deletions.
16 changes: 8 additions & 8 deletions ToDoList/tests/ToDoList.Test/UnitTests/DeleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ToDoList.Test.UnitTests;
public class DeleteUnitTests
{
[Fact]
public void Delete_ValidItemId_ReturnsNoContent()
public void Delete_DeleteByIdValidItemId_ReturnsNoContent()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand All @@ -25,11 +25,11 @@ public void Delete_ValidItemId_ReturnsNoContent()
// Assert
Assert.IsType<NoContentResult>(result);
repositoryMock.Received(1).ReadById(someId);
repositoryMock.Received(1).DeleteById(someId);
repositoryMock.Received(1).Delete(Arg.Any<ToDoItem>());
}

[Fact]
public void Delete_InvalidItemId_ReturnsNotFound()
public void Delete_DeleteByIdInvalidItemId_ReturnsNotFound()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand All @@ -43,11 +43,11 @@ public void Delete_InvalidItemId_ReturnsNotFound()
// Assert
Assert.IsType<NotFoundResult>(result);
repositoryMock.Received(1).ReadById(someId);
repositoryMock.Received(0).DeleteById(Arg.Any<int>()); // make sure nothing was deleted
repositoryMock.Received(0).Delete(Arg.Any<ToDoItem>()); // make sure nothing was deleted
}

[Fact]
public void Delete_AnyItemIdExceptionOccurredDuringReadById_ReturnsInternalServerError()
public void Delete_DeleteByIdExceptionOccurredDuringRepositoryReadById_ReturnsInternalServerError()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand All @@ -65,13 +65,13 @@ public void Delete_AnyItemIdExceptionOccurredDuringReadById_ReturnsInternalServe
}

[Fact]
public void Delete_AnyItemIdExceptionOccurredDuringDeleteById_ReturnsInternalServerError()
public void Delete_DeleteByIdExceptionOccurredDuringRepositoryDelete_ReturnsInternalServerError()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
var controller = new ToDoItemsController(repositoryMock);
repositoryMock.ReadById(Arg.Any<int>()).Returns(new ToDoItem { Name = "testItem", Description = "testDescription", IsCompleted = false });
repositoryMock.When(r => r.DeleteById(Arg.Any<int>())).Do(r => throw new Exception());
repositoryMock.When(r => r.Delete(Arg.Any<ToDoItem>())).Do(r => throw new Exception());
var someId = 1;

// Act
Expand All @@ -80,7 +80,7 @@ public void Delete_AnyItemIdExceptionOccurredDuringDeleteById_ReturnsInternalSer
// Assert
Assert.IsType<ObjectResult>(result);
repositoryMock.Received(1).ReadById(someId);
repositoryMock.Received(1).DeleteById(someId);
repositoryMock.Received(1).Delete(Arg.Any<ToDoItem>());
Assert.Equal(StatusCodes.Status500InternalServerError, ((ObjectResult)result).StatusCode);
}
}
69 changes: 69 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/GetByIdTests.cs
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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.GetByIdUnitTests.Get_ReadByIdWhenSomeItemAvailable_ReturnsOk() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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);
}

}
2 changes: 1 addition & 1 deletion ToDoList/tests/ToDoList.Test/UnitTests/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Get_ReadWhenNoItemAvailable_ReturnsNotFound()
}

[Fact]
public void Get_ReadAllExceptionOccurred_ReturnsInternalServerError()
public void Get_ReadUnhandledException_ReturnsInternalServerError()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand Down
6 changes: 4 additions & 2 deletions ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ToDoList.Test.UnitTests;
public class PostUnitTests
{
[Fact]
public void Post_ValidRequest_ReturnsNewItem()
public void Post_CreateValidRequest_ReturnsCreatedAtAction()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand All @@ -30,6 +30,8 @@ public void Post_ValidRequest_ReturnsNewItem()
// Assert
Assert.IsType<CreatedAtActionResult>(resultResult);
repositoryMock.Received(1).Create(Arg.Any<ToDoItem>());

// These asserts are optional
Assert.NotNull(value);

Assert.Equal(request.Description, value.Description);
Expand All @@ -38,7 +40,7 @@ public void Post_ValidRequest_ReturnsNewItem()
}

[Fact]
public void Post_UnhandledException_Returns500()
public void Post_CreateUnhandledException_ReturnsInternalServerError()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand Down
84 changes: 84 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/PutTests.cs
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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.PutUnitTests.Put_UpdateByIdWhenItemUpdated_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.PutUnitTests.Put_UpdateByIdWhenIdNotFound_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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);
}
}

0 comments on commit 9b87f8a

Please sign in to comment.