polypush135
So I have a module that has a private method.
defp encrypt_token(token) do
:crypto.hmac(:sha256, BeffectWeb.Endpoint.config(:secret_key_base), token)
|> Base.encode16(case: :lower)
end
its used in a method that I would like to write a test for.
def find_invite!(invite_token) do
Repo.get_by!(User, invite_token: encrypt_token(invite_token))
end
My test would need to create a fake user who has a invite_token that was encrypted the same way the private encrypt_token/1 encodes
test "find_invite!/1 returns a user with a given invite_token" do
token = "SHOULD_BE_HMAC"
# token needs to be encrypted the same way as the encrypt_token
# when passing it to the fixture, but I can't invoke the private method encrypt_token/1
user = user_fixture(invite_token: token) # invite_token: encrypt_token(token)
assert Accounts.find_invite!(token) == user
end
My question is what is the best pattern for this since I don’t want to test private functions but I do want to make sure my test uses the same methods as the real method so that way my tests don’t become brittle.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
bobbypriambodo
Extract that function to its own module and make it public?
You can even test the implementation of encrypt_token function that way if you want.
polypush135
I’ve struggled with that bit about making it public, while yes in this case you could but then why have any private methods? Should I make a method public just because I want to test it?
polypush135
Another very interesting point just made in slack was to make private methods available while only in tests.
I very much like this idea. I wonder what bad side effects could arise from this.
LostKobrakai
It’s less about private vs public but also about separating dependencies. Having a own module to do the encoding does allow you to test the encoding in isolation as well as being able to inject an mock for the encryption module on your user handling. In other words find_invite should not really be concerned with how your token is created, it just needs to be able to match tokens.
polypush135
Thats totally fare, My only hesitation for extraction was that nothing else in my app (“with exception to my test”) needed that method.
chulkilee
If it’s worth to test
encrypt_tokenseparately:find_invitetest, use the function so that you do not test the behavior ofencrypt_tokeninfind_invitetestHowever, if this is only place, and you do not want to expose it, then you should NOT use the implementation in tests. If you do so, then you’re not testing the behavior of
encrypt_token! In this case, you have to have to manually create the encrypt token and use it on test, so that even you change the implementation, the whole behavior remains same.OvermindDL1
You can also check mix to figure out if
defordefpshould be used, or make atestfunction wrapped in an if Mix.env == :test block at module-level that can be called as well.I’m partial to the
export_allapproach for testing from my old erlang work though. ^.^LostKobrakai
I’d just like to add this one here, because it’s where I got the ideas from: The Deep Synergy Between Testability and Good Design
polypush135
Another very good point made in slack is
NobbZ
There is the package
privateavailable on hex.pm. Perhaps it helps you…I’ve never worked with it, but I do have the feeling that it was announced here a while back, but I am unable to find the post…