How to mock functions from Elixir built-in modules

I’m still in a learning phase regarding testing in Elixir, and yesterday I was trying to understand the common patterns to mock built-in modules

I have the following module + function:

defmodule FizzBuzz do
  def build(file_name) do
    file_name
    |> File.read()
    |> process_file_result
  end

And in the test, I’d like to mock the File.read result in order to not have to read from my filesystem:

 describe("build/1") do
    test "when a valid file is provided, returns the converted list" do
      expected_response = {:ok, [2, :buzz, :buzz, :fizzbuzz, :buzz, :buzz]} 
      assert FizzBuzz.build("numbers.txt") == expected_response
    end

I searched for a couple of libraries and found Mox, can it be used for that purpose?

1 Like