Skip to content

Commit fb710d4

Browse files
committed
test mock setup refactoring
1 parent ca87a19 commit fb710d4

File tree

8 files changed

+27
-50
lines changed

8 files changed

+27
-50
lines changed

test/bot_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ defmodule Test.Telegram.Bot do
44
alias Test.Webhook
55
import Test.Utils.{Const, Mock}
66

7-
setup_all do
8-
Test.Utils.Mock.tesla_mock_global_async()
9-
:ok
10-
end
11-
12-
setup [:setup_test_bot]
7+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
8+
setup :setup_test_bot
139

1410
test "basic flow" do
1511
url_test_response = tg_url(tg_token(), "testResponse")

test/chat_bot_get_chat_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ defmodule Test.Telegram.ChatBotGetChat do
44
alias Test.Webhook
55
import Test.Utils.{Const, Mock}
66

7-
setup_all do
8-
Test.Utils.Mock.tesla_mock_global_async()
9-
:ok
10-
end
11-
12-
setup [:setup_test_bot]
7+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
8+
setup :setup_test_bot
139

1410
test "updates" do
1511
url_test_response = tg_url(tg_token(), "testResponse")

test/chat_bot_info_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ defmodule Test.Telegram.ChatBotInfo do
44
alias Test.Webhook
55
import Test.Utils.{Const, Mock}
66

7-
setup_all do
8-
Test.Utils.Mock.tesla_mock_global_async()
9-
:ok
10-
end
11-
12-
setup [:setup_test_bot]
7+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
8+
setup :setup_test_bot
139

1410
test "info message" do
1511
url_test_response = tg_url(tg_token(), "testResponse")

test/chat_bot_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ defmodule Test.Telegram.ChatBot do
44
alias Test.Webhook
55
import Test.Utils.{Const, Mock}
66

7-
setup_all do
8-
Test.Utils.Mock.tesla_mock_global_async()
9-
:ok
10-
end
11-
12-
setup [:setup_test_bot]
7+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
8+
setup :setup_test_bot
139

1410
test "updates" do
1511
url_test_response = tg_url(tg_token(), "testResponse")

test/chat_bot_timeout_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ defmodule Test.Telegram.ChatBotTimeout do
44
alias Test.Webhook
55
import Test.Utils.{Const, Mock}
66

7-
setup_all do
8-
Test.Utils.Mock.tesla_mock_global_async()
9-
:ok
10-
end
11-
12-
setup [:setup_test_bot]
7+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
8+
setup :setup_test_bot
139

1410
test "timeout" do
1511
url_test_response = tg_url(tg_token(), "testResponse")

test/poller_test.exs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@ defmodule Test.Telegram.Poller do
1111
end
1212
end
1313

14-
setup_all do
15-
Test.Utils.Mock.tesla_mock_global_async()
16-
:ok
17-
end
18-
19-
setup _context do
20-
bots = [{TestBotBehaviour, [token: tg_token(), max_bot_concurrency: 1]}]
21-
start_supervised!({Telegram.Poller, bots: bots})
22-
23-
:ok
24-
end
14+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
15+
setup :setup_test_bot
2516

2617
test "basic flow" do
2718
url_get_updates = tg_url(tg_token(), "getUpdates")
@@ -127,4 +118,11 @@ defmodule Test.Telegram.Poller do
127118
end
128119
)
129120
end
121+
122+
defp setup_test_bot(_context) do
123+
bots = [{TestBotBehaviour, [token: tg_token(), max_bot_concurrency: 1]}]
124+
start_supervised!({Telegram.Poller, bots: bots})
125+
126+
:ok
127+
end
130128
end

test/support/utils.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ defmodule Test.Utils.Mock do
1515
@moduledoc false
1616

1717
alias Test.Utils.Const
18+
import ExUnit.Assertions
1819

1920
@retry_wait_period Application.compile_env(:telegram, :get_updates_poll_timeout_s) * 1_000 + 500
2021

21-
def tesla_mock_global_async do
22+
def setup_tesla_mock_global_async(_context) do
2223
Tesla.Mock.mock_global(fn %{url: url} = request ->
2324
test_pid = get_test_pid_from_request_url(url)
2425

@@ -29,11 +30,13 @@ defmodule Test.Utils.Mock do
2930
response
3031
end
3132
end)
33+
34+
:ok
3235
end
3336

3437
defmacro tesla_mock_expect_request(request_pattern, fun_process_req_resp, no_pending_requests \\ true) do
3538
quote do
36-
assert_receive({:tesla_mock_request, mock_pid, request = unquote(request_pattern)}, unquote(@retry_wait_period))
39+
assert_receive {:tesla_mock_request, mock_pid, request = unquote(request_pattern)}, unquote(@retry_wait_period)
3740

3841
try do
3942
unquote(fun_process_req_resp).(request)
@@ -54,7 +57,7 @@ defmodule Test.Utils.Mock do
5457

5558
defmacro tesla_mock_refute_request(request_pattern) do
5659
quote do
57-
refute_receive({:tesla_mock_request, mock_pid, request = unquote(request_pattern)}, unquote(@retry_wait_period))
60+
refute_receive {:tesla_mock_request, mock_pid, request = unquote(request_pattern)}, unquote(@retry_wait_period)
5861
:ok
5962
end
6063
end

test/webhook_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ defmodule Test.Telegram.Webhook do
44
alias Test.Webhook
55
import Test.Utils.{Const, Mock}
66

7-
setup_all do
8-
Test.Utils.Mock.tesla_mock_global_async()
9-
:ok
10-
end
11-
12-
setup [:setup_test_bot]
7+
setup_all {Test.Utils.Mock, :setup_tesla_mock_global_async}
8+
setup :setup_test_bot
139

1410
test "basic flow" do
1511
url_test_response = tg_url(tg_token(), "testResponse")

0 commit comments

Comments
 (0)