【发布时间】:2018-11-04 07:34:45
【问题描述】:
我对在 Elixir 中实现 Genserver 还是很陌生。我有一个案例,我正在尝试管理一些状态,而 Genserver 非常适合这种情况。但是,在测试 Genserver 时,我遇到了一些复杂情况。
我有两个测试似乎相互碰撞。我对我的 Genserver 进行了单元级测试,并进行了更高级别的测试,其中我调用的函数将 Genserver 与模块一起使用。这是我的两个测试用例:
第一:
defmodule MyApp.ScoreTableQueueTest do
use MyApp.DataCase
alias MyApp.{ScoreTableQueue}
setup do
start_supervised(ScoreTableQueue)
:ok
end
test "pushes value in the queue" do
assert :ok == ScoreTableQueue.push([1,2,3,4])
end
test "pops the full value of the queue" do
assert [[1,2,3,4]] == ScoreTableQueue.pop()
end
end
如果我单独运行它,它每次都会通过。但是如果我运行这个测试,它会定期中断:
第二:
setup do
start_supervised(ScoreTableQueue)
:ok
end
describe "distribute" do
test "it distrbutes the correct season points" do
{:ok, table} = List.first(MyApp.ScoreTableAllocator.distribute())
assert table.table_details.information == [
%{team_id: team_3.id, team_score: "N/A"},
%{team_id: team_2.id, team_score: ps_2.score},
%{team_id: team_1.id, team_score: ps_1.score}
]
assert table.question_id == question.id
assert table.season_id == season.id
end
end
在我的distribute/1 函数中,Genserver 实际被使用了。如果我单独调用这个测试,它每次都可以正常工作。但是当我一起运行测试时,它似乎打破了一半的时间,让我相信我正在启动同一台服务器并将信息在测试之间传递到同一台服务器。
我的问题:如何将每个测试彼此分开?我希望每个测试对于每个案例都有一个完全不同的服务器,至少每个文件。 Elixir 的实现方式是什么?
【问题讨论】:
标签: elixir