This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I am new to xUnit and generally unit testing. I am really confused about how to write tests for this. This is basically a class from a data access assembly from a project I am working on.
public class CSVPlayerReader : IPlayerReader
{
private const string PLAYERBIO_PATH = @"xxxx\Data\PlayerBio.csv";
private const string PLAYERH2H_PATH = @"xxxx\Data\PlayerH2H.csv";
public CSVPlayerReader()
{
}
public IEnumerable<PlayerBio> GetAllPlayerBios()
{
var reader = GetReader(PLAYERBIO_PATH);
return (reader.GetRecords<PlayerBio>());
}
public IEnumerable<PlayerH2H> GetAllPlayerH2Hs()
{
var reader = GetReader(PLAYERH2H_PATH);
return reader.GetRecords<PlayerH2H>();
}
public PlayerBio GetPlayerBio(int PlayerId)
{
var reader = GetReader(PLAYERBIO_PATH);
var allPlayerBios = reader.GetRecords<PlayerBio>().ToList();
PlayerBio pb = allPlayerBios.Find(p => p.Id == PlayerId);
return pb;
}
public IEnumerable<PlayerH2H> GetPlayerH2Hs(int Player1Id, int Player2Id)
{
var reader = GetReader(PLAYERH2H_PATH);
var allPlayerH2Hs = reader.GetRecords<PlayerH2H>().Where(hh => (hh.Player1Id == Player1Id) && (hh.Player2Id == Player2Id));
return allPlayerH2Hs;
}
private CsvReader GetReader(string Path)
{
var streamReader = new StreamReader(Path);
var csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture);
return csvReader;
}
}
So for example while testing method GetAllPlayerBios, I would like to check whether it throws the correct exception if it cannot find the file or there is some permissions issue etc. But since the file paths are stored in private members, I am not sure what should be done. I have also read opinions that file or sql reading classes don't need to be unit tested and integration tests should be more than enough. So I am not sure what to do here.
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/csharp/comm...