Skip to content

Commit

Permalink
Merge pull request #98 from 0GiS0/fix/cobertura
Browse files Browse the repository at this point in the history
Update Dockerfile to use .NET 9.0 and improve test coverage reporting in CI workflow 🛠️
  • Loading branch information
0GiS0 authored Dec 29, 2024
2 parents 0d5e92d + dd67e93 commit bf618cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/devcontainers/dotnet:8.0
FROM mcr.microsoft.com/devcontainers/dotnet:9.0

# Install SQL Tools: SQLPackage and sqlcmd
COPY mssql/installSQLtools.sh installSQLtools.sh
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ jobs:
- 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
- name: Run tests with coverage
run: dotnet test --collect:"XPlat Code Coverage"

- name: Upload coverage report
uses: actions/upload-artifact@v3
with:
name: coverage-report
path: src/coverage.cobertura.xml
path: tests/TestResults/**/coverage.cobertura.xml

- name: Generate coverage summary
run: |
echo "## Coverage Report" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
cat src/coverage.cobertura.xml >> $GITHUB_STEP_SUMMARY
cat tests/TestResults/**/coverage.cobertura.xml >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
23 changes: 12 additions & 11 deletions tests/HeroControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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;
/// <summary>
/// Mock repository for the IHeroRepository interface used for testing purposes.
/// </summary>
private readonly Mock<IHeroRepository> _mockHeroRepository;
private readonly HeroController _controller;

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

[Fact]
Expand All @@ -25,7 +26,7 @@ public void GetHeroes_ReturnsOkResult_WithListOfHeroes()
new Hero("Superman", "Clark Kent"),
new Hero("Batman", "Bruce Wayne")
};
_mockRepo.Setup(repo => repo.GetAll()).Returns(heroes);
_mockHeroRepository.Setup(repo => repo.GetAll()).Returns(heroes);

// Act
var result = _controller.GetHeroes();
Expand All @@ -41,7 +42,7 @@ public void GetHero_ReturnsOkResult_WithHero()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");
_mockRepo.Setup(repo => repo.GetById(1)).Returns(hero);
_mockHeroRepository.Setup(repo => repo.GetById(1)).Returns(hero);

// Act
var result = _controller.GetHero(1);
Expand All @@ -56,7 +57,7 @@ public void GetHero_ReturnsOkResult_WithHero()
public void GetHero_ReturnsNotFoundResult_WhenHeroNotFound()
{
// Arrange
_mockRepo.Setup(repo => repo.GetById(1)).Returns((Hero)null);
_mockHeroRepository.Setup(repo => repo.GetById(1)).Returns((Hero)null);

// Act
var result = _controller.GetHero(1);
Expand Down Expand Up @@ -85,7 +86,7 @@ public void PutHero_ReturnsNoContentResult_WhenHeroUpdated()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");
_mockRepo.Setup(repo => repo.GetById(1)).Returns(hero);
_mockHeroRepository.Setup(repo => repo.GetById(1)).Returns(hero);

// Act
var result = _controller.PutHero(1, new Hero("Batman", "Bruce Wayne"));
Expand All @@ -98,7 +99,7 @@ public void PutHero_ReturnsNoContentResult_WhenHeroUpdated()
public void PutHero_ReturnsNotFoundResult_WhenHeroNotFound()
{
// Arrange
_mockRepo.Setup(repo => repo.GetById(1)).Returns((Hero)null);
_mockHeroRepository.Setup(repo => repo.GetById(1)).Returns((Hero)null);

// Act
var result = _controller.PutHero(1, new Hero("Batman", "Bruce Wayne"));
Expand All @@ -112,7 +113,7 @@ public void DeleteHero_ReturnsNoContentResult_WhenHeroDeleted()
{
// Arrange
var hero = new Hero("Superman", "Clark Kent");
_mockRepo.Setup(repo => repo.GetById(1)).Returns(hero);
_mockHeroRepository.Setup(repo => repo.GetById(1)).Returns(hero);

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

0 comments on commit bf618cb

Please sign in to comment.