Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # In my test env config I have this set up, as a mock for Nadia
- test.exs ->
- config :shards_of_ocarion, :tg_sender, ShardsOfOcarionWeb.Senders.TestTelegramSender
- # Here is how it is mocked
- defmodule ShardsOfOcarionWeb.Senders.TestTelegramSender do
- def send_message(_, text, _ \\ []) do
- send self, {:message_sent, text}
- end
- end
- #Here is the module that uses Nadia or that mock to send messages
- defmodule ShardsOfOcarionWeb.Senders.TelegramSender do
- def send_message(chat_id, text, options \\ []) do
- sender_module().send_message(chat_id, text, options)
- end
- defp sender_module() do
- Application.get_env(:shards_of_ocarion, :tg_sender)
- end
- end
- # And this module is in charge of sending generic messages. I want to test, that it sends after_sign_up_message
- defmodule ShardsOfOcarionWeb.Senders.Generic do
- def after_sign_up_message(chat_id, user_name) do
- text = "Добро пожаловать в игру, #{user_name}"
- ShardsOfOcarionWeb.Senders.TelegramSender.send_message(chat_id, text)
- end
- end
- # With this test. The test passes, as expected.
- defmodule ShardsOfOcarionWeb.Senders.GenericTest do
- use ExUnit.Case
- # doctest ShardsOfOcarion
- describe "#after_sign_up_message" do
- test "sends after_sign_up_message" do
- user_name = "Tharin"
- ShardsOfOcarionWeb.Senders.Generic.after_sign_up_message(1, user_name)
- assert_received {:message_sent, "Добро пожаловать в игру, Tharin"}
- end
- end
- end
- # But then I have this module
- defmodule ShardsOfOcarion.Game.Flows.Player.SignUpFlow do
- alias ShardsOfOcarion.Players
- alias ShardsOfOcarion.Players.Player
- def call(chat_id, user_name, "/start") do
- case Players.create_player(%{tg_nickname: user_name}) do
- %Player{} ->
- ShardsOfOcarionWeb.Senders.Generic.after_sign_up_message(chat_id, user_name)
- {:ok, :signed_up}
- _ ->
- ShardsOfOcarionWeb.Senders.Errors.could_not_sign_up(chat_id)
- {:error, :could_not_sign_up}
- end
- end
- end
- # And I have a dilemma. I've been taught not to excessively test everything, because it makes tests slower. If I already have after_sign_up_message covered with tests that it actually sends a message, I don't need it to run here again. It slows the test. Not significantly, but make 5000 of such tests and there is a difference, IMO. As I've got this covered with tests, all I need to test is that when .create_player returns a %Player{}, a message is being sent afterwards. As I've already said, I come from Ruby world and have a habit to allow().to receive() . What can I do here? Maybe, my code is just bad and there is a proper way to write it, so it becomes easily testable? I can't inject every module everywhere as a dependency to override it with test-purpose modules as I did with TestTelegramSender module. Am I right, that I should only do that for external dependencies like Nadia? Then how should I test this if using libraries such as Mock is highly discouraged? I googled the hell out of the internet and the most useful thing I found was this link: https://bhserna.com/testing-functions-with-side-effects-in-elixir.html . It helped me to test my Senders.Generic module, but it didn't tell me how to test modules that use it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement