I want to write unit tests for my project and I was wondering is there any way to check if any file was being created in specific directory and if it's, how many lines is consisting of? Thanks again! Here is some code of the project here.
Asked
Active
Viewed 2,225 times
-2
-
3`File.Exists()`? Can you show us what you have so far, and what is not working? – gunr2171 Jul 07 '14 at 17:03
-
You can do that by checking if the file exists and reading its content. Don't look to far for the solution. Also, this is integration testing. – Jeroen Vannevel Jul 07 '14 at 17:04
-
yes you can do all that. – Bit Jul 07 '14 at 17:05
-
Everything is working perfectly. I have an alogrithm that is writing a combination at each line and I want to assert that the file is created and is consisted of N number of lines. – Stanimir Yakimov Jul 07 '14 at 17:05
-
2@StanimirYakimov, you have not shown us any code yet, so it sounds like you want us to write the unit test for you. It's better if you give it a shot yourself, then come to use when your attempt does not work. – gunr2171 Jul 07 '14 at 17:06
-
@gunr2171, okey, no problem, take the code https://github.com/stambeto09/Personal-ID-Generator – Stanimir Yakimov Jul 07 '14 at 17:07
-
1Please edit your post to include any additional information you have to your question. Avoid adding this in the comments, as they are harder to read and can be deleted easier. The edit button for your post is just below the post's tags. – gunr2171 Jul 07 '14 at 17:07
-
1as @gunr2171 said you can use File.Exists() to assert the file is created also the File class has a method that you can determine number of lines with: `var lineCount = File.ReadLines(@"C:\file.txt").Count();` will give you # of lines quickly. – AR5HAM Jul 07 '14 at 17:08
-
Okay, thanks for answers, but I still can't understand why you demoted me. First of all, I think that it's not necesary to show any code in each of my question. Second @gunr2171, why do you think something is working? – Stanimir Yakimov Jul 07 '14 at 17:26
-
I'm downvoting for blatant lack of research shown. I'm close voting because you are having a problem (your code does not unit test something) and you need help solving it, but you are not giving us enough information about your code base. I could have also said "this question is too broad". If you would like to continue discussing this question, please go to [meta]. – gunr2171 Jul 07 '14 at 17:30
3 Answers
5
You don't want to be creating files as part of your test -- the file system falls under the category of an external dependency. If you interact with the real file system, your test becomes an integration test, not a unit test.
What you can do in this case is mediate all of your file system access through a thin wrapper class represented by an interface, then test against that.
For example:
public interface IFileSystem
{
void WriteAllText(string filePath, string fileContents);
bool Exists(string filePath);
}
public class RealFileSystem : IFileSystem
{
public void WriteAllText(string filePath, string fileContents)
{
File.WriteAllText(filePath, fileContents);
}
public void Exists(string filePath)
{
return File.Exists(filePath);
}
}
public class TestFileSystem : IFileSystem
{
public Dictionary<string, string> fileSystem = new Dictionary<string, string>();
public void WriteAllText(string filePath, string fileContents)
{
fileSystem.Add(filePath, fileContents);
}
public void Exists(string filePath)
{
return fileSystem.ContainsKey(filePath);
}
}

Daniel Mann
- 57,011
- 13
- 100
- 120
3
To see if the file exists, use File.Exists():
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
To see how many lines are in the file, count the iterations of File.ReadLines() (per this answer):
var lineCount = File.ReadLines(@"C:\file.txt").Count();
0
You'd probably just want to stick this if statement at the end of your tests
if (File.Exists("file/path") && File.ReadLines("file/path").Count()
== your_number_of_lines)
{
//passed!
}

gunr2171
- 16,104
- 25
- 61
- 88

Dylan Corriveau
- 2,561
- 4
- 29
- 36