ExUnit assertions for comparing data structures by identity.
AssertIdentity is available on Hex.
The package can be installed by adding assert_identity
to your list of
dependencies in mix.exs
:
def deps do
[
{:assert_identity, "~> 0.1.0", only: :test}
]
end
Check the documentation for more details.
AssertIdentity is designed to be used with ExUnit. It provides assertions to check that two inputs have the same identity. This is useful, for example, when dealing with Ecto structs which may not be strictly equivalent due to, e.g. association preloading.
For convenience, you can import the helpers in your test case:
def MyApp.DataCase do
use ExUnit.CaseTemplate
using do
quote do
import MyApp.DataCase
import AssertIdentity
end
end
end
Or otherwise just directly in your test:
def MyApp.ModuleTest do
import AssertIdentity
end
Use the assert_ids_match
function to check that two maps, structs, or lists of
the former have the same identity (i.e., the same values for the :id
key). You
can optionally specify the key name if not :id
.
def MyApp.PostTest do
use MyApp.DataCase
test "list_posts/0 returns all posts" do
post = insert(:post)
assert_ids_match(Posts.list_posts(), [post])
end
end
Contributions are welcome! To make changes, clone the repo, make sure tests pass, and then open a PR on GitHub.
git clone https://github.com/newaperio/assert_identity.git
cd assert_identity
mix deps.get
mix test
AssertIdentity is Copyright © 2020 NewAperio. It is free software, and may be redistributed under the terms specified in the LICENSE file.
AssertIdentity is built by NewAperio, LLC.
NewAperio is a web and mobile design and development studio. We offer expert Elixir and Phoenix development as part of our portfolio of services. Get in touch to see how our team can help you.