DockYard / valid_field

https://hex.pm/packages/valid_field
MIT License
48 stars 6 forks source link
elixir testing

ValidField Build Status Inline docs

ValidField is built and maintained by DockYard, contact us for expert Elixir and Phoenix consulting.

ValidField allows you to unit test changesets

Usage

Add valid_field to your list of dependencies in mix.exs:

def deps do
  [{:valid_field, "~> 0.6.0", only: :test}]
end

Then in your unit test:

defmodule App.UserTest do
  import ValidField
  alias App.User

  test ".changeset - Validations" do
    with_changeset(%User{})
    |> assert_valid_field(:email, ["something@else.com"])
    |> assert_invalid_field(:email, ["", nil, "test"])
    |> assert_valid_field(:password, ["password123!"])
    |> assert_invalid_field(:password, [nil, "", "test", "nospecialcharacters1", "nonumber!"])
  end
end

Alternatively you can combine the assert_valid_field/3 and assert_invalid_field/3 syntax into assert_field/4. Refactoring the same example above using assert_field/4 would yield:

defmodule App.UserTest do
  import ValidField
  alias App.User

  test ".changeset - Validations" do
    with_changeset(%User{})
    |> assert_field(:email, ["something@else.com"], ["", nil, "test"])
    |> assert_field(:password, ["password123!"], [nil, "", "test", "nospecialcharacters1", "nonumber!"])
  end
end

Authors

We are very thankful for the many contributors

Versioning

This library follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard, Inc. © 2016

@dockyard

Licensed under the MIT license