forked from adomokos/light-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_expects_and_promises_spec.rb
93 lines (81 loc) · 2.39 KB
/
action_expects_and_promises_spec.rb
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
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'spec_helper'
describe ":expects and :promises macros" do
describe "actions are backward compatible" do
class FooAction
extend LightService::Action
executed do |context|
baz = context.fetch :baz
bar = baz + 2
context[:bar] = bar
end
end
it "works without expects and promises" do
result = FooAction.execute(:baz => 3)
expect(result).to be_success
expect(result[:bar]).to eq(5)
end
end
context "when expected keys are not in context" do
class FooNoExpectedKeyAction
extend LightService::Action
expects :baz
executed do |context|
baz = context.fetch :baz
bar = baz + 2
context[:bar] = bar
end
end
it "throws an ExpectedKeysNotInContextError" do
# FooAction invoked with nothing in the context
expect { FooNoExpectedKeyAction.execute }.to \
raise_error(LightService::ExpectedKeysNotInContextError)
end
end
describe "expected keys" do
class FooWithReaderAction
extend LightService::Action
expects :baz
executed do |context|
# Notice how I use `context.baz` here
bar = context.baz + 2
context[:bar] = bar
end
end
it "can be accessed through a reader" do
result = FooWithReaderAction.execute(:baz => 3)
expect(result).to be_success
expect(result[:bar]).to eq(5)
end
end
context "when promised keys are not in context" do
class FooNoPromisedKeyAction
extend LightService::Action
expects :baz
promises :bar
executed do |context|
# I am not adding anything to the context
end
end
it "throws a PromisedKeysNotInContextError" do
# FooAction invoked with nothing placed in the context
expect { FooNoPromisedKeyAction.execute(:baz => 3) }.to \
raise_error(LightService::PromisedKeysNotInContextError)
end
end
describe "promised keys" do
class FooWithExpectsAndPromisesAction
extend LightService::Action
expects :baz
promises :bar
executed do |context|
# Notice how I use `context.bar` here
context.bar = context.baz + 2
end
end
it "puts the value through the accessor into the context" do
result = FooWithExpectsAndPromisesAction.execute(:baz => 3)
expect(result).to be_success
expect(result[:bar]).to eq(5)
end
end
end