Skip to content

Commit

Permalink
Merge pull request #92 from 0GiS0/add-unit-tests-1aa
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
0GiS0 authored Dec 28, 2024
2 parents 21985f3 + 63f1ecc commit 276b4ed
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"ms-azuretools.vscode-docker",
"redhat.vscode-yaml",
"GitHub.copilot",
"GitHub.copilot-chat"
"GitHub.copilot-chat",
"redhat.vscode-xml"
]
}
},
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/dotnet.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ jobs:
dotnet-version: 8.x
- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore

- name: Test
run: dotnet test --no-build --verbosity normal

- name: Install coverlet
run: dotnet tool install --global coverlet.console

- name: Run tests with coverage
run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
25 changes: 25 additions & 0 deletions tests/HealthControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Xunit;
using tour_of_heroes_api.Controllers;
using Microsoft.AspNetCore.Mvc;

public class HealthControllerTests
{
private readonly HealthController _controller;

public HealthControllerTests()
{
_controller = new HealthController();
}

[Fact]
public void Get_ReturnsOkResult_WithHealthyMessage()
{
// Act
var result = _controller.Get();

// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var message = Assert.IsType<string>(okResult.Value);
Assert.Equal("Healthy", message);
}
}
123 changes: 123 additions & 0 deletions tests/HeroControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Collections.Generic;
using Moq;
using Xunit;
using tour_of_heroes_api.Controllers;
using tour_of_heroes_api.Models;
using Microsoft.AspNetCore.Mvc;

public class HeroControllerTests
{
private readonly Mock<IHeroRepository> _mockRepo;
private readonly HeroController _controller;

public HeroControllerTests()
{
_mockRepo = new Mock<IHeroRepository>();
_controller = new HeroController(_mockRepo.Object);
}

[Fact]
public void GetHeroes_ReturnsOkResult_WithListOfHeroes()
{
// Arrange
var heroes = new List<Hero>
{
new Hero("Superman", "Clark Kent"),
new Hero("Batman", "Bruce Wayne")
};
_mockRepo.Setup(repo => repo.GetAll()).Returns(heroes);

// Act
var result = _controller.GetHeroes();

// Assert
var okResult = Assert.IsType<OkObjectResult>(result.Result);
var returnHeroes = Assert.IsType<List<Hero>>(okResult.Value);
Assert.Equal(2, returnHeroes.Count);
}

[Fact]
public void GetHero_ReturnsOkResult_WithHero()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");
_mockRepo.Setup(repo => repo.GetById(1)).Returns(hero);

// Act
var result = _controller.GetHero(1);

// Assert
var okResult = Assert.IsType<OkObjectResult>(result.Result);
var returnHero = Assert.IsType<Hero>(okResult.Value);
Assert.Equal("Superman", returnHero.Name);
}

[Fact]
public void GetHero_ReturnsNotFoundResult_WhenHeroNotFound()
{
// Arrange
_mockRepo.Setup(repo => repo.GetById(1)).Returns((Hero)null);

// Act
var result = _controller.GetHero(1);

// Assert
Assert.IsType<NotFoundResult>(result.Result);
}

[Fact]
public void PostHero_ReturnsOkResult_WithCreatedHero()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");

// Act
var result = _controller.PostHero(hero);

// Assert
var okResult = Assert.IsType<OkObjectResult>(result.Result);
var returnHero = Assert.IsType<Hero>(okResult.Value);
Assert.Equal("Superman", returnHero.Name);
}

[Fact]
public void PutHero_ReturnsNoContentResult_WhenHeroUpdated()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");
_mockRepo.Setup(repo => repo.GetById(1)).Returns(hero);

// Act
var result = _controller.PutHero(1, new Hero("Batman", "Bruce Wayne"));

// Assert
Assert.IsType<NoContentResult>(result);
}

[Fact]
public void PutHero_ReturnsNotFoundResult_WhenHeroNotFound()
{
// Arrange
_mockRepo.Setup(repo => repo.GetById(1)).Returns((Hero)null);

// Act
var result = _controller.PutHero(1, new Hero("Batman", "Bruce Wayne"));

// Assert
Assert.IsType<NotFoundResult>(result);
}

[Fact]
public void DeleteHero_ReturnsNoContentResult_WhenHeroDeleted()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");
_mockRepo.Setup(repo => repo.GetById(1)).Returns(hero);

// Act
var result = _controller.DeleteHero(1);

// Assert
Assert.IsType<NoContentResult>(result);
}
}
28 changes: 28 additions & 0 deletions tests/tour-of-heroes-api.tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\tour-of-heroes-api.csproj" />
</ItemGroup>

</Project>

0 comments on commit 276b4ed

Please sign in to comment.