如何在不破坏管道的情况下在管道中间模拟函数?

时间:2016-06-05 10:28:01

标签: elixir

我为API客户端制作了这段代码:

create_request
|> emit

在生产中emit()使用配置中指定的http客户端(httpoisonApplication.get_env(…)。在测试环境中,http客户端不是httpoison,而是我制作的模块:

def request({method, url, body, headers}) do
  send(self(), {method, url, body, headers})
end
然后我的测试:

test "Emits the request via the http client specified in the environment config" do
  MyClient.emit({:get, "http://google.com", nil, nil})
  assert_received({:get, "http://google.com, nil, nil})
end

此测试通过。

现在我要解析响应。所以我在管道中添加了一个函数:

create_request
|> emit
|> parse_response

现在我的测试不再通过了。 parse_response的输入应该是HTTPoison.Response。在生产中管道工作,但在我的测试中,因为假的http客户端只是send输入self()它不输出符合{{期望的输入类型的值1}}

我有什么选择?

  1. 在生产代码中向process_response添加一个子句,以便处理该特殊情况(仅在测试中发生)。为了可测试性而不是改变生产代码的忠实粉丝
  2. 类似于1)但使用协议?
  3. 选项3

0 个答案:

没有答案