Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
test(back-end): Add Certificates controller tests and add new service…
Browse files Browse the repository at this point in the history
…s controller test

See also: #119
  • Loading branch information
CarlosPavajeau committed Apr 21, 2021
1 parent 186783a commit 09f713c
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Kaizen.Test/Controllers/CertificatesControllerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Kaizen.Controllers;
using Kaizen.Domain.Entities;
using Kaizen.Domain.Repositories;
using Kaizen.Models.Certificate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;

namespace Kaizen.Test.Controllers
{
public class CertificatesControllerTest : BaseControllerTest
{
private CertificatesController _certificatesController;
private Mock<ICertificatesRepository> _certificatesRepository;

[SetUp]
public void SetUp()
{
_certificatesRepository = new Mock<ICertificatesRepository>();

_certificatesController =
new CertificatesController(_certificatesRepository.Object, ServiceProvider.GetService<IMapper>());

SetUpCertificatesRepository();
}

private void SetUpCertificatesRepository()
{
_certificatesRepository.Setup(r => r.GetClientCertificates("1007870922")).ReturnsAsync(
new List<Certificate>
{
new()
{
Id = 1,
Validity = DateTime.Now.AddDays(10),
WorkOrderCode = 1
},
new()
{
Id = 2,
Validity = DateTime.Now.AddDays(15),
WorkOrderCode = 2
}
});

_certificatesRepository.Setup(r => r.FindByIdAsync(1)).ReturnsAsync(new Certificate
{
Id = 1,
Validity = DateTime.Now.AddDays(10),
WorkOrderCode = 1
});
_certificatesRepository.Setup(r => r.FindByIdAsync(3)).ReturnsAsync((Certificate) null);
}

[Test]
public async Task Get_Client_Certificates()
{
var result = (await _certificatesController.GetClientCertificates("1007870922")).Result as OkObjectResult;

Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
Assert.IsInstanceOf<IEnumerable<CertificateViewModel>>(result.Value);

var value = result.Value as IEnumerable<CertificateViewModel>;
Assert.IsNotNull(value);
Assert.AreEqual(2, value.Count());
}

[Test]
public async Task Get_Existing_Certificate()
{
var result = await _certificatesController.GetCertificate(1);

Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
Assert.AreEqual(1, result.Value.Id);
}

[Test]
public async Task Get_Non_Existent_Certificate()
{
var result = await _certificatesController.GetCertificate(3);

Assert.IsNotNull(result);
Assert.IsNull(result.Value);

Assert.IsInstanceOf<NotFoundObjectResult>(result.Result);
}
}
}
14 changes: 14 additions & 0 deletions Kaizen.Test/Controllers/ServicesControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private void SetUpServicesRepository()
_servicesRepository.Setup(r => r.Update(It.IsAny<Service>())).Verifiable();

_servicesRepository.Setup(r => r.Insert(It.IsAny<Service>())).Verifiable();
_servicesRepository.Setup(r => r.Insert(It.IsAny<ServiceType>())).Verifiable();
}

private void SetUpUnitWork()
Expand Down Expand Up @@ -199,5 +200,18 @@ public async Task Save_Existing_Service()
Assert.IsNull(result.Value);
Assert.IsInstanceOf<ConflictObjectResult>(result.Result);
}

[Test]
public async Task Save_New_ServiceType()
{
var result = await _servicesController.ServiceTypes(new ServiceTypeInputModel
{
Name = "Control de plagas"
});

Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
Assert.AreEqual("Control de plagas", result.Value.Name);
}
}
}

0 comments on commit 09f713c

Please sign in to comment.