testing - 在 Elixir 中生成响应数据时避免模拟

标签 testing mocking elixir

我开始明白,在 Elixir 社区中,mock 通常没有得到很好的考虑。考虑到这一点,我有以下困境,我不太确定如何解决。

首先,我目前的设计使用 https://github.com/jjh42/mock

remote_api_generated_data = %{
  transaction_id: "09200-12-235089721-3987235",
  confirmation_number: "JB789FKS78GS82"
}

with_mock RemoteApi.Client, [order: Mocks.RemoteApi.Client.successful_transaction(&1, vaulted_data)] do
  conn = post conn, transaction_path(conn, :create), data: [@create_attrs]
  response = json_response(conn, 201)["data"]
  assert response.composite_key == "#{remote_api_generated_data.transaction_id}:#{remote_api_generated_data.confirmation_number}"
end

José Valim 使用协议(protocol)和配置文件提供了一个很好的模拟解决方案。详情见帖子:http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts

我的问题是:如何采用“协议(protocol)和配置更改”的方法并仍然验证远程生成的数据?(认识到这不是数据本身,而是数据的转换我断言。在这种情况下,composite_key)

最佳答案

我发现有用的方法是使用类似 ex_vcr 的工具在系统的边界,仅用于确保远程数据可以反序列化为定义明确的结构。

这就像一个集成测试,将使用来自远程系统的缓存响应。

所有应用程序业务逻辑和数据转换都可以定义为可以直接测试的普通老式 elixir 模块和函数。

关于testing - 在 Elixir 中生成响应数据时避免模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46259135/

相关文章:

angularjs - 测试 Angular 应用程序时遇到问题/错误 : 'DEPRECATION: describe with no children (describe() or it())

javascript - node.js 模拟 sendmailer 传输器内部函数

c# - 使用 Moq 模拟扩展方法

elixir - 字符串操作 : join with comma

elixir - => 和 : in an Elixir map? 有什么区别

php - 如何使用 PHP 检查远程文件是否存在?

javascript - 类型错误 : Cannot set property '_eventListeners' of undefined

python - 确认函数内部的函数调用在使用模拟模块的单元测试中

python - 使用 pd.read_sql 模拟多个数据库调用

elixir - IEx.Helper.h 的终端友好输出,同时输出到非 TTY 的内容