diff --git a/README.md b/README.md index 7cf9658..ee1deac 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,12 @@ def project do end ``` +## Integrating a testing library + +By default, `use WhiteBread.Context` will import ExUnit.Assertions. If you're not using ExUnit, you'll probably want to override this default by calling `use WhiteBread.Context, test_library: :some_other_library_name`. + +At the moment, the only library names available are `:ex_unit` (same as the default), `:espec`, and `nil` (which skips the test library setup step altogether). + # Next steps - Additional Suites and subcontexts After following the getting started steps you may find your default context starts to get a bit large. diff --git a/lib/white_bread/context.ex b/lib/white_bread/context.ex index 2e04072..9e650f8 100644 --- a/lib/white_bread/context.ex +++ b/lib/white_bread/context.ex @@ -4,12 +4,16 @@ defmodule WhiteBread.Context do alias WhiteBread.Context.Setup @step_keywords [:given_, :when_, :then_, :and_, :but_] + @default_test_library :ex_unit @doc false - defmacro __using__(_opts) do + defmacro __using__(opts \\ []) do + opts = Keyword.merge [test_library: @default_test_library], opts + [test_library: test_library] = opts + quote do import WhiteBread.Context - import ExUnit.Assertions + unquote(import_test_library test_library) @behaviour WhiteBread.ContextBehaviour @@ -110,5 +114,15 @@ defmodule WhiteBread.Context do end end - + defp import_test_library(test_library) do + case test_library do + :ex_unit -> quote do: import ExUnit.Assertions + :espec -> quote do + require ESpec + use ESpec + end + nil -> quote do: true + _ -> raise ArgumentError, "#{inspect test_library} is not a recognized value for :test_library. Recognized values are :ex_unit, :espec, and nil." + end + end end