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
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…
5
LostKobrakai
Popular in Discussions
Following https://github.com/tbrand/which_is_the_fastest |>
https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
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
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
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
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
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
Hi everyone, im working on find best language/framework/system for
high concurrency, high performance and stable performance
after wor...
New
Other popular topics
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...
New
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









