Skip to content

Instantly share code, notes, and snippets.

@joshchernoff joshchernoff/mix test
Last active Oct 29, 2016

Embed
What would you like to do?
validate_format does not look to be working.
# Note that slug is "$#_asDFDF"
# and should not be valid.
# yet I see that I get an ok on insert.
# FYI I'm using ex_machina thus why you see build/2
1) test changeset is invalid when slug contains restricted characters (Digitalcakes.PostTest)
test/models/post_test.exs:32
match (=) failed
code: {:error, changeset} = Repo.insert(post)
rhs: {:ok,
%Digitalcakes.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "posts">, body: "foo bar jones", id: 185, inserted_at: #Ecto.DateTime<2016-10-29 21:00:38>,
published_at: #Ecto.DateTime<2016-10-29 21:00:38>, slug: "$#_asDFDF", title: "Test Post 5", updated_at: #Ecto.DateTime<2016-10-29 21:00:38>}}
stacktrace:
test/models/post_test.exs:34: (test)
...
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title, :slug, :body, :published_at])
|> validate_required([:title, :slug, :body])
|> validate_format(:slug, ~r/^[a-z0-9-]+$/) # is this working?
end
...
...
test "changeset is invalid when slug contains restricted characters" do
post = build(:post, %{slug: "$#_asDFDF"}) |> Post.changeset()
assert {:error, changeset} = Repo.insert(post)
assert changeset.errors[:slug] == {"is invalid", []}
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.