Eu não entendi bem como funciona esse esquema do construtor e do IDisposable, não sei se eu também fiz a modificação certa na minha classe.
Só o QuandoArquivoExistenteDeveRetornarUmaListaDePets() passou, os outros não.
using Alura.Adopet.Console.Modelos;
using Alura.Adopet.Console.Util;
namespace Alura.Adopet.Testes;
public class LeitorDeArquivoTeste : IDisposable
{
private string caminhoArquivo;
public LeitorDeArquivoTeste()
{
//Setup
string linha = "456b24f4-19e2-4423-845d-4a80e8854a41;Lima Limão;1";
File.WriteAllText("lista.csv", linha);
caminhoArquivo = Path.GetFullPath("lista.csv");
}
[Fact]
public void QuandoArquivoExistenteDeveRetornarUmaListaDePets()
{
//Arrange
//Act
var listaDePets = new LeitorDeArquivo(caminhoArquivo).RealizaLeitura()!;
//Assert
Assert.NotNull(listaDePets);
Assert.Single(listaDePets);
Assert.IsType<List<Pet>?>(listaDePets);
}
[Fact]
public void QuandoArquivoNaoExistenteDeveRetornarNulo()
{
//Arrange
//Act
var listaDePets = new LeitorDeArquivo("").RealizaLeitura();
//Assert
Assert.Null(listaDePets);
}
[Fact]
public void QuandoArquivoForNuloDeveRetornarNulo()
{
//Arrange
//Act
var listaDePets = new LeitorDeArquivo(null).RealizaLeitura();
//Assert
Assert.Null(listaDePets);
}
public void Dispose()
{
//ClearDown
File.Delete(caminhoArquivo);
}
}
Classe
using Alura.Adopet.Console.Modelos;
namespace Alura.Adopet.Console.Util;
public class LeitorDeArquivo
{
private readonly string _caminhoDoArquivo;
public LeitorDeArquivo(string? caminhoDoArquivo = "")
{
_caminhoDoArquivo = caminhoDoArquivo!;
}
public List<Pet> RealizaLeitura()
{
if(_caminhoDoArquivo == null || _caminhoDoArquivo.Trim() == "")
{
throw new Exception("Caminho do arquivo não pode ser nulo ou vazio.");
}
List<Pet> listaDePets = new List<Pet>();
using (StreamReader sr = new StreamReader(_caminhoDoArquivo))
{
System.Console.WriteLine("----- Serão importados os dados abaixo -----");
while (!sr.EndOfStream)
{
var linha = sr.ReadLine()!;
var pet = linha.ConverteDoTexto();
listaDePets.Add(pet);
}
}
return listaDePets;
}
}
/*
* Documentação de diretrizes para coleções:
* https://learn.microsoft.com/pt-br/dotnet/standard/design-guidelines/guidelines-for-collections
*/