Skip to content

Commit 27ccd8d

Browse files
test: add unit tests for CepService and ViaCepService with mocked dependencies
1 parent c05cada commit 27ccd8d

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

Application/Service/CepService.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using mail_api.Data;
1+
using mail_api.Application.Service;
2+
using mail_api.Data;
23
using mail_api.Domain.DTO;
3-
using mail_api.Domain.Model;
4-
using Newtonsoft.Json;
54
using mail_api.Domain.Interfaces;
5+
using mail_api.Domain.Model;
66
using Microsoft.Extensions.Logging;
7+
using Newtonsoft.Json;
78

89
namespace mail_api.Service
910
{
@@ -14,14 +15,15 @@ public class CepService : ICepService
1415
private readonly ICepRepository _cepRepository;
1516
private readonly ILogger<CepService> _logger;
1617

17-
public CepService( ICepRepository repository)
18+
public CepService(IViaCepService viaCepService, ICepRepository repository, ILogger<CepService> logger)
1819
{
19-
20-
this._cepRepository = repository ?? throw new ArgumentNullException(nameof(repository));
20+
_viaCepService = viaCepService ?? throw new ArgumentNullException(nameof(viaCepService));
21+
_cepRepository = repository ?? throw new ArgumentNullException(nameof(repository));
22+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2123
}
2224

2325

24-
26+
2527
public async Task<CepInfo> GetByCep(string cep)
2628
{
2729

Application/Service/ViaCepService.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public async Task<CepInfo> FetchAddressByCep(cepRequest cepRequest)
4545
_logger.LogError($"HttpRequestException occurred while fetching address for CEP {cepRequest.Cep}: {ex.Message}");
4646
throw new Exception("Error obtaining address by CEP.", ex);
4747
}
48-
catch (Exception ex)
49-
{
50-
_logger.LogError($"Unexpected error occurred while fetching address for CEP {cepRequest.Cep}: {ex.Message}");
51-
throw new Exception("Unexpected error processing request.", ex);
52-
}
5348
}
5449
}
5550
}

Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using mail_api.Data;
22
using mail_api.Domain.Interfaces;
33
using mail_api.Application.Service;
4+
using mail_api.Service;
45

56

67
var builder = WebApplication.CreateBuilder(args);

mail-api.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.10.35013.160
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mail-api", "mail-api.csproj", "{8375F62A-013D-46FF-843F-2B6C4ED00589}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MailApiTest", "..\MailApiTest\MailApiTest.csproj", "{7D5390DC-17AE-4ED1-8855-2700AD34E4F3}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{8375F62A-013D-46FF-843F-2B6C4ED00589}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{8375F62A-013D-46FF-843F-2B6C4ED00589}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{8375F62A-013D-46FF-843F-2B6C4ED00589}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{7D5390DC-17AE-4ED1-8855-2700AD34E4F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{7D5390DC-17AE-4ED1-8855-2700AD34E4F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{7D5390DC-17AE-4ED1-8855-2700AD34E4F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{7D5390DC-17AE-4ED1-8855-2700AD34E4F3}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)