Skip to content

Commit

Permalink
Po Lekci 07
Browse files Browse the repository at this point in the history
  • Loading branch information
Hijtec committed Nov 7, 2024
1 parent 759f39b commit 03aab65
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
13 changes: 9 additions & 4 deletions ToDoList/src/ToDoList.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

var builder = WebApplication.CreateBuilder(args);
{
//Configure DI
builder.Services.AddControllers();
builder.Services.AddDbContext<ToDoItemsContext>();
builder.Services.AddScoped<IRepository<ToDoItem>, ToDoItemsRepository>();
//WebApi services
builder.Services.AddControllers(); // pridalo ToDoItemsController
builder.Services.AddSwaggerGen();

//Persistence services
builder.Services.AddDbContext<ToDoItemsContext>(); // pridalo ToDoItemsContext
builder.Services.AddScoped<IRepository<ToDoItem>, ToDoItemsRepository>(); // pridalo ToDoItemsRepository
}

var app = builder.Build();
{
//Configure Middleware (HTTP request pipeline)
app.MapControllers();
app.UseSwagger();
app.UseSwaggerUI(config => config.SwaggerEndpoint("/swagger/v1/swagger.json", "ToDoList API V1"));
}

app.Run();
5 changes: 5 additions & 0 deletions ToDoList/src/ToDoList.WebApi/ToDoList.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<ProjectReference Include="..\ToDoList.Persistence\ToDoList.Persistence.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.9.0" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
Expand Down
73 changes: 73 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/GetTests.cs
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);
}
}
6 changes: 3 additions & 3 deletions ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ToDoList.Test;
namespace ToDoList.Test.UnitTests;

using NSubstitute;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -11,7 +11,7 @@ namespace ToDoList.Test;
public class PostUnitTests
{
[Fact]
public void Post_ValidRequest_ReturnsNewItem()
public void Post_CreateValidRequest_ReturnsCreatedAtAction()

Check warning on line 14 in ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.PostUnitTests.Post_CreateValidRequest_ReturnsCreatedAtAction() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand All @@ -38,7 +38,7 @@ public void Post_ValidRequest_ReturnsNewItem()
}

[Fact]
public void Post_UnhandledException_Returns500()
public void Post_CreateUnhandledException_ReturnsInternalServerError()

Check warning on line 41 in ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.PostUnitTests.Post_CreateUnhandledException_ReturnsInternalServerError() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
Expand Down

0 comments on commit 03aab65

Please sign in to comment.