forked from antonmi/espec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
before_test.exs
80 lines (66 loc) · 2 KB
/
before_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
defmodule BeforeTest do
use ExUnit.Case, async: true
defmodule SomeSpec do
use ESpec
before do: {:ok, a: 1}
it do: "#{shared[:a]} is defined"
context "Context" do
before do: {:ok, a: 10, b: 2}
it do: "#{shared[:a]} and #{shared[:b]} is defined"
ESpec.Context.describe "Describe" do
before do: {:ok, b: fn(a) -> a * 2 end}
it do: "#{shared[:b].(10)} == 20"
end
end
context "'shared is available" do
before b: shared[:a] + 1
it do: "b = #{shared[:b]}"
end
context "error or throw" do
context "throw term" do
before do: throw :some_term
it do: true
end
context "fail " do
before do: raise "Error"
it do: true
end
end
end
setup_all do
{:ok,
ex1: Enum.at(SomeSpec.examples, 0),
ex2: Enum.at(SomeSpec.examples, 1),
ex3: Enum.at(SomeSpec.examples, 2),
ex4: Enum.at(SomeSpec.examples, 3),
ex5: Enum.at(SomeSpec.examples, 4),
ex6: Enum.at(SomeSpec.examples, 5)
}
end
test "run ex1", context do
example = ESpec.ExampleRunner.run(context[:ex1])
assert(example.result == "1 is defined")
end
test "run ex2", context do
example = ESpec.ExampleRunner.run(context[:ex2])
assert(example.result == "10 and 2 is defined")
end
test "run ex3", context do
example = ESpec.ExampleRunner.run(context[:ex3])
assert(example.result == "20 == 20")
end
test "run ex4", context do
example = ESpec.ExampleRunner.run(context[:ex4])
assert(example.result == "b = 2")
end
test "run example which throws term", context do
example = ESpec.ExampleRunner.run(context[:ex5])
assert example.status == :failure
assert String.match?(example.error.message, ~r/throw :some_term/)
end
test "run example which raises error", context do
example = ESpec.ExampleRunner.run(context[:ex6])
assert example.status == :failure
assert String.match?(example.error.message, ~r/\(RuntimeError\) Error/)
end
end