Skip to content

Instantly share code, notes, and snippets.

@nessamurmur
Last active August 29, 2015 14:20
Show Gist options
  • Save nessamurmur/81281be8ddfd976b7566 to your computer and use it in GitHub Desktop.
Save nessamurmur/81281be8ddfd976b7566 to your computer and use it in GitHub Desktop.
defmodule MyApp.User do
alias MyApp.Repo
## Stuff omitted
def changeset(model, params) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_unique(:username, on: Repo)
end
end
defmodule MyApp.UserTest do
# Stuff omitted
@valid_attrs %{username: "thedude"}
test "validates uniqueness of username" do
changeset = User.changeset(%User{}, @valid_attrs)
Repo.insert(changeset)
refute changeset.valid?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment