I am new and this feels like a stupid question so please just hear me out. I have a project that will work its way through a directory and find any video files in non-video folders and visa versa. I want to put some testing in but I have never done serious testing before. How would I go about it? And wouldn’t testing be impossible since the result could change when I send it to my client or whenever any of the folders I am using for testing change? Or is this just a problem where I have to just deal with it?
It sounds like you are thinking that you should be testing against “production” data. This is not the case. In testing, you assert against some fixed, deterministic data that you setup.
For example, based on a very quick look at your code, you could set up the following directory structure to test against:
defmodule ErovfTest do
use ExUnit.Case
describe "main_function/1" do
test "finds video files" do
dir = "test/test_dirs/dir1"
files = main_function("test/support/dir1")
assert files == ["file3.mov"]
end
test "finds no video files" do
dir = "test/test_dirs/dir2"
files = main_function(dir)
assert Enum.empty?(files)
end
end
end
Thank you so much for the feedback guys! I appreciate the example a ton sodapopcan. I do have a couple of questions when it comes to verbiage for kokolegorille, what is a side effect function? What do you mean by boundary and core?
For this one the __DIR__ special form can be your friend. You can root the directory of files to whatever directory your test is in, which will make your code organization better than if you root it to a subdirectory of test/
Absolutely. As soon as I posted this I realized how I was referencing the directories was a little sloppy.
In practice, for this type of thing, I would do what @kokolegorille is suggesting. In generally, I wouldn’t want to have to set up directories of files for testing but rather pass data structures representing the filesystem to the functional core. I wanted to keep the example simple and inline with the repo provided, though.