Skip to content

Commit ae67be2

Browse files
Add README
1 parent d6e0eeb commit ae67be2

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

README.md

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
1-
EctoValidationCase
1+
Ecto.ValidationCase
22
==================
33

4-
** TODO: Add description **
4+
Simplify your Ecto model validation tests. Loosely inspired by [shoulda
5+
matchers][shoulda], but simpler.
6+
7+
## Sample
8+
9+
```elixir
10+
defmodule MyApp.UserTest do
11+
use ExUnit.Case
12+
use Ecto.ValidationCase, model: MyApp.User
13+
14+
test "requires password to be 10 chars long" do
15+
rejects "password", for: :password, message: "too short"
16+
accepts "password123", for: :password
17+
end
18+
end
19+
```
20+
21+
## Rationale
22+
23+
Rather than create a matching test helper function for each of the valiations
24+
that [Ecto][ecto] supports, this library intentionally keeps things simple,
25+
providing only `accepts/2` and `rejects/2`. Why?
26+
27+
- It's very easy to remember, which makes validation tests more likely to be
28+
written.
29+
- The tests are more explicit, with a minimum of magic. It is very clear exactly
30+
what values are being tested, for which fields, and which error messages
31+
should be returned.
32+
33+
## Installation
34+
35+
You can install `Ecto.ValidationCase` from hex. First, change your `deps` in
36+
`mix.exs`:
37+
38+
```elixir
39+
def deps do
40+
[{:ecto_validation_case, ">= 0.1.0"}]
41+
end
42+
```
43+
44+
Then run `mix deps.get` to install.
45+
46+
[ecto]: https://github.com/elixir-lang/ecto
47+
[shoulda]: http://matchers.shoulda.io/

lib/ecto/validation_case.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule Ecto.ValidationCase do
2-
@shortdoc "Simplify testing Ecto validations."
2+
@shortdoc "Simplify testing Ecto model validations."
33

44
@moduledoc """
55
Ecto.ValidationCase simplifies writing validation tests for Ecto models.
@@ -12,7 +12,7 @@ defmodule Ecto.ValidationCase do
1212
alias MyApp.User
1313
1414
test "requires password to be 10 chars long" do
15-
rejects "password", for: [User, :password]
15+
rejects "password", for: [User, :password], message: "too short"
1616
accepts "password123", for: [User, :password]
1717
end
1818
end
@@ -28,8 +28,8 @@ defmodule Ecto.ValidationCase do
2828
2929
use Ecto.ValidationCase, model: MyApp.User
3030
31-
You can then leave the model out of the `for` option, and `MyApp.User` will be
32-
used by default.
31+
You can then leave the model out of the `for:` option, and `MyApp.User` will
32+
be used by default.
3333
3434
accepts "Daniel Berkompas", for: :name
3535

0 commit comments

Comments
 (0)