zyra 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Zyra::Builder do
6
- subject(:builder) { described_class.new(model_class) }
7
-
8
- let(:model_class) { User }
9
-
10
- describe '#build' do
11
- it do
12
- expect(builder.build).to be_a(model_class)
13
- end
14
-
15
- it do
16
- expect(builder.build)
17
- .not_to be_persisted
18
- end
19
-
20
- context 'when attributes are given' do
21
- let(:name) { SecureRandom.hex(10) }
22
-
23
- it 'initializes the model with the given attribute' do
24
- expect(builder.build(name: name).name)
25
- .to eq(name)
26
- end
27
- end
28
-
29
- context 'when a block is given' do
30
- let(:name) { SecureRandom.hex(10) }
31
-
32
- it 'initializes the model with the given attribute' do
33
- value = name
34
- expect(builder.build { |model| model.name = value }.name)
35
- .to eq(name)
36
- end
37
- end
38
-
39
- context 'when building has an after build handler' do
40
- let(:name) { SecureRandom.hex(10) }
41
-
42
- before do
43
- value = name
44
- builder.after(:build) { |model| model.name = value }
45
- end
46
-
47
- it 'runs the event handler' do
48
- expect(builder.build.name)
49
- .to eq(name)
50
- end
51
- end
52
- end
53
-
54
- describe '#create' do
55
- it do
56
- expect(builder.create).to be_a(model_class)
57
- end
58
-
59
- it do
60
- expect(builder.create)
61
- .to be_persisted
62
- end
63
-
64
- context 'when attributes are given' do
65
- let(:name) { SecureRandom.hex(10) }
66
-
67
- it 'initializes the model with the given attribute' do
68
- expect(builder.create(name: name).name)
69
- .to eq(name)
70
- end
71
- end
72
-
73
- context 'when a block is given' do
74
- let(:name) { SecureRandom.hex(10) }
75
-
76
- it 'initializes the model with the given attribute' do
77
- value = name
78
- expect(builder.create { |model| model.name = value }.name)
79
- .to eq(name)
80
- end
81
- end
82
-
83
- context 'when building has an after build handler' do
84
- let(:name) { SecureRandom.hex(10) }
85
-
86
- before do
87
- value = name
88
- builder.after(:build) { |model| model.name = "#{value}#{model.id}" }
89
- end
90
-
91
- it 'runs the event handler' do
92
- expect(builder.create.name)
93
- .to eq(name)
94
- end
95
- end
96
-
97
- context 'when building has an after create handler' do
98
- let(:name) { SecureRandom.hex(10) }
99
-
100
- before do
101
- value = name
102
- builder.after(:create) { |model| model.name = "#{value}#{model.id}" }
103
- end
104
-
105
- it 'runs the event handler' do
106
- model = builder.create
107
- expect(model.name)
108
- .to eq("#{name}#{model.id}")
109
- end
110
- end
111
- end
112
-
113
- describe '#after' do
114
- let(:name) { SecureRandom.hex(10) }
115
-
116
- it 'register a handler to be ran after an event' do
117
- value = name
118
-
119
- expect { builder.after(:build) { |model| model.name = value } }
120
- .to change { builder.build.name }
121
- .from(nil).to(name)
122
- end
123
-
124
- it do
125
- expect(builder.after(:build) {})
126
- .to be(builder)
127
- end
128
- end
129
- end