testing - exunit 测试用例的可覆盖宏

标签 testing elixir phoenix-framework ex-unit

我正在为我的应用程序编写测试用例,我的大部分 Controller 都包含 CRUD 的通用代码,因此我编写了通用宏并在我的 Controller 中使用它。所有 Controller 的测试用例都将自动编写。但是我对如何使这个通用代码可重写感到困惑,以便我可以随时重写。

defmodule Qserv.ControllerTest do
  defmacro __using__(_options) do
   quote location: :keep do
     use Qserv.Web.ConnCase, async: true
     # this kernel will give me access to current `@model` and `@controller` 
     use Qserv.KernelTest

     describe "#{@controller}.create/2" do
       test "All required fields set `required` in model should generate errors that these fields are missing -> One, two, All"

       test "Only required fields should create record and match the object"
     end

     # defoverridable index: 2, I want to override above `describe` completely or/and the included test cases
   end
 end
end

任何帮助/想法如何实现这一目标?

最佳答案

我通常不喜欢“让我们稍后再做一些事情来撤消它”。它通常会迫使开发人员在他们的头脑中保留一个堆栈,以了解以后如何添加和删除内容。

特别是在这种情况下,您要耦合测试名称。想象一下,有人决定将“一、二、全部”中的“二”变成大写。现在所有 future 的覆盖都将不适用,您将有重复的测试。

明确选择所需内容的更好解决方案。例如,您可以定义在必要时使用的更小的宏:

describe_create!
describe_update!
...
describe_delete!

也许您可以使用 describe_restful! 来调用所有这些。这里的教训是在其上构建小的构建 block ,而不是稍后尝试分解的大块。

PS:请使用比我使用的 describe_x 更好的名称。 :)

关于testing - exunit 测试用例的可覆盖宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48759626/

相关文章:

php - 如何跳过 PHPUnit 中的错误测试?

c# - 更改 DeploymentItem 的名称

java - Mockito 的验证和参数捕获器的工作

android - 测试 Android 应用程序 - 如何执行数据库数据设置?

erlang - 未为结构实现可枚举协议(protocol)。如何将结构转换为可枚举?

elixir - 在 Elixir 中将数字转换为单词

Elixir 在线 IDE/playground 网站

elixir - 管道图正在尝试调用原始函数

erlang - 在 phoenix controller test 中使用 fixture 创建用户并在 @valid_attrs 中使用 id

phoenix-framework - Ecto 模型 - 选择中的子查询