-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from 0GiS0/add-unit-tests-1aa
Add unit tests
- Loading branch information
Showing
5 changed files
with
186 additions
and
1 deletion.
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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> |