简体   繁体   English

exunit 测试用例的可覆盖宏

[英]Overridable Macro for exunit test cases

I am writing test cases for my application and most of my controllers contain common code for CRUDs so I have written common macro and use it inside my controllers.我正在为我的应用程序编写测试用例,我的大多数控制器都包含 CRUD 的通用代码,所以我编写了通用宏并在我的控制器中使用它。 Test cases for all of the controllers would be written automatically.所有控制器的测试用例都将自动编写。 But I am confused about how to make this common code overridable so that I can override whenever I want.但是我很困惑如何使这个通用代码可覆盖,以便我可以随时覆盖。

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

Any help/idea how to achieve this?任何帮助/想法如何实现这一目标?

I am generally not a fan of the "let's do things to undo it later".我通常不喜欢“让我们稍后再撤消它”。 It generally forces developers to keep a stack in their head of how things are added and removed later on.它通常迫使开发人员在他们的脑海中保留一个堆栈,以了解以后如何添加和删除内容。

In this case in particular, you are coupling on the test name.特别是在这种情况下,您正在耦合测试名称。 Imagine someone decides to make the "two" in "One, two, All" uppercase.想象一下,有人决定将“一,二,全部”中的“二”变成大写。 Now all of the future overrides won't apply and you will have duplicate tests.现在所有未来的覆盖都将不适用,您将有重复的测试。

A better solution to explicit opt in what you need.显式选择所需内容的更好解决方案。 For example, you can define smaller macros that you use when necessary:例如,您可以定义在必要时使用的较小的宏:

describe_create!
describe_update!
...
describe_delete!

Maybe you could have describe_restful!也许你可以有describe_restful! that invokes all of them.调用所有这些。 The lesson here is to have small building blocks that you build on top of instead of having a huge chunk that you try to break apart later.这里的教训是在上面构建小的构建块,而不是稍后尝试分解的大块。

PS: please use better names than the describe_x that I used. PS:请使用比我使用的describe_x更好的名称。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM