簡體   English   中英

exunit 測試用例的可覆蓋宏

[英]Overridable Macro for exunit test cases

我正在為我的應用程序編寫測試用例,我的大多數控制器都包含 CRUD 的通用代碼,所以我編寫了通用宏並在我的控制器中使用它。 所有控制器的測試用例都將自動編寫。 但是我很困惑如何使這個通用代碼可覆蓋,以便我可以隨時覆蓋。

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

任何幫助/想法如何實現這一目標?

我通常不喜歡“讓我們稍后再撤消它”。 它通常迫使開發人員在他們的腦海中保留一個堆棧,以了解以后如何添加和刪除內容。

特別是在這種情況下,您正在耦合測試名稱。 想象一下,有人決定將“一,二,全部”中的“二”變成大寫。 現在所有未來的覆蓋都將不適用,您將有重復的測試。

顯式選擇所需內容的更好解決方案。 例如,您可以定義在必要時使用的較小的宏:

describe_create!
describe_update!
...
describe_delete!

也許你可以有describe_restful! 調用所有這些。 這里的教訓是在上面構建小的構建塊,而不是稍后嘗試分解的大塊。

PS:請使用比我使用的describe_x更好的名稱。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM