This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(back-end): Add ServiceInvoices controller tests
See also: # 119
- Loading branch information
1 parent
68b8768
commit e70c3ab
Showing
1 changed file
with
163 additions
and
0 deletions.
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
Kaizen.Test/Controllers/ServiceInvoicesControllerTest.cs
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,163 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Kaizen.Controllers; | ||
using Kaizen.Domain.Entities; | ||
using Kaizen.Domain.Repositories; | ||
using Kaizen.Models.ServiceInvoice; | ||
using Kaizen.Test.Helpers; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Kaizen.Test.Controllers | ||
{ | ||
[TestFixture] | ||
public class ServiceInvoicesControllerTest : BaseControllerTest | ||
{ | ||
private ServiceInvoicesController _serviceInvoicesController; | ||
private Mock<IServiceInvoicesRepository> _serviceInvoicesRepository; | ||
private Mock<IUnitWork> _unitWork; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_serviceInvoicesRepository = new Mock<IServiceInvoicesRepository>(); | ||
_unitWork = new Mock<IUnitWork>(); | ||
|
||
_serviceInvoicesController = new ServiceInvoicesController(_serviceInvoicesRepository.Object, | ||
_unitWork.Object, ServiceProvider.GetService<IMapper>()); | ||
|
||
SetUpServiceInvoicesRepository(); | ||
SetUpUnitWork(); | ||
} | ||
|
||
private void SetUpServiceInvoicesRepository() | ||
{ | ||
_serviceInvoicesRepository.Setup(r => r.GetAll()).Returns(new TestAsyncEnumerable<ServiceInvoice>( | ||
new List<ServiceInvoice> | ||
{ | ||
new() | ||
{ | ||
Id = 1, | ||
ClientId = "1007870922", | ||
Client = new Client | ||
{ | ||
Id = "1007870922" | ||
} | ||
}, | ||
new() | ||
{ | ||
Id = 2, | ||
ClientId = "1007870921", | ||
Client = new Client | ||
{ | ||
Id = "1007870921" | ||
} | ||
} | ||
})); | ||
|
||
_serviceInvoicesRepository.Setup(r => r.FindByIdAsync(1)).ReturnsAsync(new ServiceInvoice | ||
{ | ||
Id = 1, | ||
ClientId = "1007870922", | ||
Client = new Client | ||
{ | ||
Id = "1007870922" | ||
} | ||
}); | ||
|
||
_serviceInvoicesRepository.Setup(r => r.FindByIdAsync(3)).ReturnsAsync((ServiceInvoice)null); | ||
|
||
_serviceInvoicesRepository.Setup(r => r.GetClientInvoices("1007870922")) | ||
.ReturnsAsync(new List<ServiceInvoice> | ||
{ | ||
new() | ||
{ | ||
Id = 1, | ||
ClientId = "1007870922", | ||
Client = new Client | ||
{ | ||
Id = "1007870922" | ||
} | ||
} | ||
}); | ||
|
||
_serviceInvoicesRepository.Setup(r => r.Update(It.IsAny<ServiceInvoice>())).Verifiable(); | ||
} | ||
|
||
private void SetUpUnitWork() | ||
{ | ||
_unitWork.Setup(r => r.SaveAsync()).Returns(Task.CompletedTask); | ||
} | ||
|
||
[Test] | ||
public async Task Get_ServiceInvoices() | ||
{ | ||
OkObjectResult result = (await _serviceInvoicesController.GetServiceInvoices()).Result as OkObjectResult; | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsNotNull(result.Value); | ||
Assert.IsInstanceOf<IEnumerable<ServiceInvoiceViewModel>>(result.Value); | ||
} | ||
|
||
[Test] | ||
public async Task Get_Existing_ServiceInvoice() | ||
{ | ||
ActionResult<ServiceInvoiceViewModel> result = await _serviceInvoicesController.GetServiceInvoice(1); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsNotNull(result.Value); | ||
Assert.AreEqual(1, result.Value.Id); | ||
} | ||
|
||
[Test] | ||
public async Task Get_Non_Existent_ServiceInvoice() | ||
{ | ||
ActionResult<ServiceInvoiceViewModel> result = await _serviceInvoicesController.GetServiceInvoice(3); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsNull(result.Value); | ||
Assert.IsInstanceOf<NotFoundObjectResult>(result.Result); | ||
} | ||
|
||
[Test] | ||
public async Task Get_ClientInvoices() | ||
{ | ||
OkObjectResult result = (await _serviceInvoicesController.ClientInvoices("1007870922")).Result as OkObjectResult; | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsNotNull(result.Value); | ||
Assert.IsInstanceOf<IEnumerable<ServiceInvoiceViewModel>>(result.Value); | ||
} | ||
|
||
[Test] | ||
public async Task Update_Existing_ServiceInvoice() | ||
{ | ||
ActionResult<ServiceInvoiceViewModel> result = await _serviceInvoicesController.PutServiceInvoice(1, new ServiceInvoiceEditModel | ||
{ | ||
PaymentMethod = PaymentMethod.Cash, | ||
State = InvoiceState.Paid | ||
}); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsNotNull(result.Value); | ||
Assert.AreEqual(PaymentMethod.Cash, result.Value.PaymentMethod); | ||
} | ||
|
||
[Test] | ||
public async Task Update_Non_Existent_ServiceInvoice() | ||
{ | ||
ActionResult<ServiceInvoiceViewModel> result = await _serviceInvoicesController.PutServiceInvoice(3, new ServiceInvoiceEditModel | ||
{ | ||
PaymentMethod = PaymentMethod.Cash, | ||
State = InvoiceState.Paid | ||
}); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsNull(result.Value); | ||
Assert.IsInstanceOf<NotFoundObjectResult>(result.Result); | ||
} | ||
} | ||
} |
e70c3ab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#119