View Source Doumi.Test

Hex Version Hex Docs License Last Updated

Doumi.Test is a helper library that make it easier to test your Elixir codes.

도우미(Doumi) means "helper" in Korean.

Usage

assert_same_values/2

defmodule MyApp.Test do
  use ExUnit.Case
  use Doumi.Test

  test "returns true with the same values" do
    number1 = 1
    number2 = 1.0

    datetime1 = ~U[2023-11-11 00:00:00Z]
    datetime2 = ~U[2023-11-11 00:00:00.000Z]

    decimal1 = Decimal.new("1.1")
    decimal2 = 1.1

    # instead of
    assert number1 == number2
    assert DateTime.compare(datetime1, datetime2) == :eq
    assert Decimal.equal?(decimal1, decimal2)

    # do
    assert_same_values number1, number2
    assert_same_values datetime1, datetime2
    assert_same_values decimal1, decimal2
  end
end

assert_same_fields/3

defmodule MyApp.Test do
  use ExUnit.Case
  use Doumi.Test

  test "returns true with the same fields" do
    attrs = %{
      name: "name",
      age: 30,
      married: false
    }

    assert {:ok, something} = create_something(attrs)

    # instead of
    assert something.name == attrs.name
    assert something.age == attrs.age
    assert something.married == attrs.married

    # do
    assert_same_fields something, attrs, [:name, :age, :married]
  end
end

assert_same_records/2

defmodule MyApp.Test do
  use MyApp.DataCase
  use Doumi.Test

  test "returns true with the same records" do
    something = insert(:something)

    assert {:ok, fetched_something} = fetch_something(something.primary_key0, something.primary_key1)

    # instead of
    assert fetched_something.primary_key0 == something.primary_key0
    assert fetched_something.primary_key1 == something.primary_key1

    # do
    assert_same_records fetched_something, something
  end
end

Installation

The package can be installed by adding doumi_test to your list of dependencies in mix.exs:

def deps do
  [
    {:doumi_test, "~> 0.1.0"}
  ]
end

Doumi*

All Doumi libraries:

  • Doumi.Port: A helper library that makes it easier to use Python, Ruby in Elixir
  • Doumi.Phoenix.SVG: A helper library that generates Phoenix function components from SVG files.
  • Doumi.Phoenix.Params: A helper library that supports converting form to params and params to form
  • Doumi.URI.Query: A helper library that encode complicated query of URI.

Copyright (c) 2023 Jinkyou Son (Json)

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.