LauraBeatris

LauraBeatris

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?

Most Liked

al2o3cr

al2o3cr

I’m probably missing some nuance due to the example being generic, but it sounds like what you want is a test of process_file_result, not one for build:

  • you want to set an input string for it during test setup
  • you want to inspect the output

No mocks needed in that case…

LostKobrakai

LostKobrakai

By convention @moduledoc false modules are to be considered private.

tfwright

tfwright

Yes a very common pattern is to create your own API for “file reading functions”, use that instead of File directly, and then you can mock your API in tests as covered in this post

Where Next?

Popular in Discussions Top

mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement