polypush135

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.

Showing Posts 1 to 10

bobbypriambodo

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

polypush135 OP

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

polypush135 OP

Another very interesting point just made in slack was to make private methods available while only in tests.

i use the export_all erlang compilation directive to make them available in test suites
you just include erlc_opts: [ :export_all ] in mix.exs (for Mix.env == test or whatever)

  • alisdair

I very much like this idea. I wonder what bad side effects could arise from this.

LostKobrakai

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

polypush135 OP

Thats totally fare, My only hesitation for extraction was that nothing else in my app (“with exception to my test”) needed that method.

chulkilee

chulkilee

If it’s worth to test encrypt_token separately:

  • make it public in own module
  • test the module
  • in find_invite test, use the function so that you do not test the behavior of encrypt_token in find_invite test

However, 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

OvermindDL1

You can also check mix to figure out if def or defp should be used, or make a test function wrapped in an if Mix.env == :test block at module-level that can be called as well.

I’m partial to the export_all approach for testing from my old erlang work though. ^.^

LostKobrakai

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

polypush135 OP

Another very good point made in slack is

I think the only way to test the contract you’ve established is to create the user via the same code-path that you would do it at runtime. I’m guessing there’s some type of create_user_by_invite/2 function or something

  • david.antaramian
NobbZ

NobbZ

There is the package private available 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…

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews