浏览代码

Added tests for technologies controller & service

pull/64/head
Ermin Bronja 3 年前
父节点
当前提交
799de2e9dd

+ 57
- 0
Diligent.WebAPI.Tests/Controllers/TechnologiesControllerTests.cs 查看文件

using Diligent.WebAPI.Contracts.DTOs.Technology;
using Diligent.WebAPI.Contracts.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Tests.Controllers
{
public class TechnologiesControllerTests
{
private ITechnologyService _technologyService = Substitute.For<ITechnologyService>();
private List<TechnologyResponseDto> _technologies = new();
private TechnologyResponseDto _technology = new();

public TechnologiesControllerTests()
{
_technology = new TechnologyResponseDto
{
TechnologyId = 1,
Name = ".NET",
TechnologyType = "Backend"
};

_technologies.Add(_technology);
}

[Fact]
public async Task GetAll_ShouldReturn200OK_WhenCalled()
{
_technologyService.GetAllAsync().Returns(_technologies);
TechnologiesController technologiesController = new(_technologyService);
var result = await technologiesController.GetAll();
(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetById_ShouldReturn200OK_WhenTechnologyExists()
{
_technologyService.GetByIdAsync(Arg.Any<int>()).Returns(_technology);
TechnologiesController technologiesController = new(_technologyService);
var result = await technologiesController.GetById(1);
(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetById_ShouldThrowEntityNotFoundException_WhenTechnologyDontExists()
{
_technologyService.When(x => x.GetByIdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

TechnologiesController technologiesController = new(_technologyService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => technologiesController.GetById(1000));
}
}
}

+ 91
- 0
Diligent.WebAPI.Tests/Services/TechnologyServiceTests.cs 查看文件

using AutoMapper;
using Diligent.WebAPI.Business.MappingProfiles;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Contracts.DTOs.Technology;
using Diligent.WebAPI.Contracts.Exceptions;
using Diligent.WebAPI.Data.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Tests.Services
{
public class TechnologyServiceTests
{
private readonly IMapper _mapper;
private readonly List<Technology> _technologies = new();
private readonly Technology _technology;

public TechnologyServiceTests()
{
var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
new List<Profile>
{
new TechnologyMappingProfile()
}));
_mapper = new Mapper(configuration);

_technology = new Technology
{
TechnologyId = 1,
Name = ".NET",
TechnologyType = TechnologyTypes.Backend
};

_technologies.Add(_technology);
}

[Fact]
public async Task GetAll_ShouldReturnListOfTechnologies_WhenCalled()
{
var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
TechnologyService tehnologyService = new(_mapper, databaseContext);

var result = await tehnologyService.GetAllAsync();

Assert.Equal(result.Count, 1);
}

[Fact]
public async Task GetById_ShouldReturnTechnology_WhenTechnologyExists()
{
var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
TechnologyService tehnologyService = new(_mapper, databaseContext);

var result = await tehnologyService.GetByIdAsync(1);

result.Should().BeEquivalentTo(_mapper.Map<TechnologyResponseDto>(_technology));
}

[Fact]
public async Task GetById_ShouldThrowEntityNotFoundException_WhenTechnologyDontExists()
{
var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
TechnologyService tehnologyService = new(_mapper, databaseContext);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await tehnologyService.GetByIdAsync(1000));
}

[Fact]
public async Task GetEntityById_ShouldReturnTechnology_WhenTechnologyExists()
{
var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
TechnologyService tehnologyService = new(_mapper, databaseContext);

var result = await tehnologyService.GetEntityByIdAsync(1);

result.Should().BeEquivalentTo(_technology);
}

[Fact]
public async Task GetEntityById_ShouldThrowEntityNotFoundException_WhenTechnologyDontExists()
{
var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
TechnologyService tehnologyService = new(_mapper, databaseContext);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await tehnologyService.GetEntityByIdAsync(1000));
}
}
}

正在加载...
取消
保存