Skip to content

Commit

Permalink
Merge pull request #2 from AlexcastroDev/feat/TestGetLocations
Browse files Browse the repository at this point in the history
test: add LocationControllerTests
  • Loading branch information
alexcastrodev authored Oct 11, 2023
2 parents 8a05c6b + 0987209 commit 8e86c67
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ https://localhost:7186/swagger/index.html

This following links are the references i read for build this project:

https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest

https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices

https://stackoverflow.com/questions/51489111/how-to-unit-test-with-actionresultt

https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-7.0&viewFallbackFrom=aspnetcore-2.2#actionresultt-type
26 changes: 26 additions & 0 deletions Tests/LocationsControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using ibge.Controllers;
using ibge.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Tests;
[TestClass]
public class LocationTest
{
[TestMethod]
public async Task Get_Locations_ReturnsEmpty()
{
// Arrange
DbContextOptions<LocationContext> options = new DbContextOptionsBuilder<LocationContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
LocationContext context = new(options);
LocationsController controller = new(context);

// Act
ActionResult<List<Location>> result = await controller.Get();

// Assert
Assert.AreEqual(0, result.Value?.Count);
}
}
22 changes: 22 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ibge\ibge.csproj" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
6 changes: 6 additions & 0 deletions ibge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{E5094833-4B8B-4AD8-B639-C52BE79199BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{2D058B54-1E07-449C-8CD5-699E9B8ED3E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D058B54-1E07-449C-8CD5-699E9B8ED3E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D058B54-1E07-449C-8CD5-699E9B8ED3E7}.Release|Any CPU.Build.0 = Release|Any CPU
{E5094833-4B8B-4AD8-B639-C52BE79199BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5094833-4B8B-4AD8-B639-C52BE79199BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5094833-4B8B-4AD8-B639-C52BE79199BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5094833-4B8B-4AD8-B639-C52BE79199BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions ibge/Controllers/LocationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public LocationsController(LocationContext context)
[HttpGet(Name = "GetListOfIbge")]
public async Task<ActionResult<List<Location>>> Get()
{
var locations = await _context.Locations.ToListAsync();
List<Location> locations = await _context.Locations.ToListAsync();

return Ok(locations);
return locations;
}
}

0 comments on commit 8e86c67

Please sign in to comment.