File tree 2 files changed +49
-6
lines changed
2 files changed +49
-6
lines changed Original file line number Diff line number Diff line change 1
- EctoValidationCase
1
+ Ecto.ValidationCase
2
2
==================
3
3
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/
Original file line number Diff line number Diff line change 1
1
defmodule Ecto.ValidationCase do
2
- @ shortdoc "Simplify testing Ecto validations."
2
+ @ shortdoc "Simplify testing Ecto model validations."
3
3
4
4
@ moduledoc """
5
5
Ecto.ValidationCase simplifies writing validation tests for Ecto models.
@@ -12,7 +12,7 @@ defmodule Ecto.ValidationCase do
12
12
alias MyApp.User
13
13
14
14
test "requires password to be 10 chars long" do
15
- rejects "password", for: [User, :password]
15
+ rejects "password", for: [User, :password], message: "too short"
16
16
accepts "password123", for: [User, :password]
17
17
end
18
18
end
@@ -28,8 +28,8 @@ defmodule Ecto.ValidationCase do
28
28
29
29
use Ecto.ValidationCase, model: MyApp.User
30
30
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.
33
33
34
34
accepts "Daniel Berkompas", for: :name
35
35
You can’t perform that action at this time.
0 commit comments