Commit b1bb7510 authored by adnansetiawan's avatar adnansetiawan
Browse files

add unit testing for location api

parent ec9d81c2
Pipeline #24320 passed with stages
in 6 minutes and 17 seconds
......@@ -42,7 +42,10 @@ namespace EasyParking.Core
}
public Location GetByName(string name)
{
return _locationRepository.GetByName(name);
var result = _locationRepository.GetByName(name);
if (result == null)
throw new Core.Exceptions.LocationNotFoundExeption();
return result;
}
}
}
using System;
using EasyParking.API.Controllers;
using EasyParking.Core;
using Microsoft.AspNetCore.Mvc;
using Xunit;
namespace EasyParking.UnitTest.API
{
public class LocationControllerTest
{
private readonly LocationController _locationController;
private readonly ILocationRepository _locationRepository;
public LocationControllerTest()
{
_locationRepository = new LocationRepositoryFake();
_locationController = new LocationController(_locationRepository);
}
[Fact]
public void Get_WhenCalled_ReturnsOkResult()
{
//act
var okResult = _locationController.Get();
//assert
Assert.IsType<OkObjectResult>(okResult);
}
[Fact]
public void GetByName_WhenCalled_ReturnsOkResult()
{
//act
var okResult = _locationController.GetByName("Location 1");
//assert
Assert.IsType<OkObjectResult>(okResult);
var actualResult = okResult as OkObjectResult;
Assert.Equal("Location 1", (actualResult.Value as EasyParking.API.Dtos.LocationDetailDto).Name);
}
[Fact]
public void GetByName_WhenCalled_ReturnsException()
{
//act
var badResult = _locationController.GetByName("Location 1X");
//assert
var actualResult = badResult as BadRequestObjectResult;
Assert.Equal(400, actualResult.StatusCode);
Assert.Equal("Location Not Found", actualResult.Value);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using EasyParking.Core;
namespace EasyParking.UnitTest.API
{
public class LocationRepositoryFake : ILocationRepository
{
private readonly List<Core.Location> _locations;
public LocationRepositoryFake()
{
_locations = new List<Core.Location>
{
new Location { Id = Guid.NewGuid(), Name = "Location 1" },
new Location { Id = Guid.NewGuid(), Name = "Location 2" },
new Location { Id = Guid.NewGuid(), Name = "Location 3" }
};
}
public void Delete(Guid Id)
{
var locationToDelete = GetById(Id);
if (locationToDelete != null)
{
_locations.Remove(locationToDelete);
}
}
public IReadOnlyList<Location> GetAll()
{
return _locations;
}
public Location GetById(Guid Id)
{
return _locations.FirstOrDefault(x => x.Id == Id);
}
public Location GetByName(string name)
{
return _locations.FirstOrDefault(x => x.Name == name);
}
public void Insert(Location data)
{
_locations.Add(data);
}
public void Update(Location data)
{
var index = _locations.IndexOf(data);
if (index > -1)
{
_locations[index] = data;
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="API\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EasyParking.API\EasyParking.API.csproj" />
<ProjectReference Include="..\EasyParking.Core\EasyParking.Core.csproj" />
</ItemGroup>
</Project>
......@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyParking.Infrastructure"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyParking.API", "EasyParking.API\EasyParking.API.csproj", "{E4E2718B-9071-47AA-ADCD-2EB385FF562A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyParking.UnitTest", "EasyParking.UnitTest\EasyParking.UnitTest.csproj", "{187EA334-97A4-44E1-8915-736F4EF74702}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -25,5 +27,9 @@ Global
{E4E2718B-9071-47AA-ADCD-2EB385FF562A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4E2718B-9071-47AA-ADCD-2EB385FF562A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4E2718B-9071-47AA-ADCD-2EB385FF562A}.Release|Any CPU.Build.0 = Release|Any CPU
{187EA334-97A4-44E1-8915-736F4EF74702}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{187EA334-97A4-44E1-8915-736F4EF74702}.Debug|Any CPU.Build.0 = Debug|Any CPU
{187EA334-97A4-44E1-8915-736F4EF74702}.Release|Any CPU.ActiveCfg = Release|Any CPU
{187EA334-97A4-44E1-8915-736F4EF74702}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment