-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.rb
More file actions
72 lines (60 loc) · 1.45 KB
/
test.rb
File metadata and controls
72 lines (60 loc) · 1.45 KB
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
class Test
def initialize(description)
@description = description
end
def given(*events)
@events = events
self
end
def when(command)
@command = command
self
end
def expect_error(error)
@expected_error = error
self
end
def expect_event(event)
@expected_event = event
self
end
attr_reader :description, :events, :command, :expected_event, :expected_error
def run(api)
api.store.append(events) unless events&.empty?
api.call(command)
if expected_error
fail("Expected error not raised")
else
check_expected_event(api.store.read.last)
end
rescue Api::Error => e
check_expected_error(e)
rescue => e
fail(e.message)
end
private
def pass(message = nil)
log("PASS", description, message && ":", message)
end
def fail(message = nil)
log("FAIL", description, message && ":", message)
end
def log(*values)
puts values.compact.join(" ")
end
def check_expected_event(actual_event)
if expected_event && actual_event.instance_of?(expected_event.class) &&
actual_event.data == expected_event.data
pass("Event published as expected")
else
fail("Expected event not published")
end
end
def check_expected_error(actual_error)
if !expected_event && actual_error.message == expected_error
pass("Expected error: " + expected_error)
else
fail("Unexpected error: " + actual_error.message)
end
end
end