Skip to content

Commit 5c4755f

Browse files
authored
Use stub instead of anonymous function. (#193)
1 parent a917755 commit 5c4755f

3 files changed

Lines changed: 305 additions & 215 deletions

File tree

handwritten/pubsub/samples/test/iam.test.js

Lines changed: 122 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -56,170 +56,206 @@ describe('pubsub:iam', function () {
5656
describe('getTopicPolicy', function () {
5757
it('should get a topic\'s policy', function () {
5858
var sample = getSample();
59-
sample.program.getTopicPolicy(topicName, function (err, policy) {
60-
assert.ifError(err);
61-
assert.equal(policy, policyMock);
62-
assert(console.log.calledWith('Got topic policy:', policyMock));
63-
});
59+
var callback = sinon.stub();
60+
61+
sample.program.getTopicPolicy(topicName, callback);
62+
63+
assert.ifError(callback.firstCall.args[0]);
64+
assert.equal(callback.firstCall.args[1], policyMock);
65+
assert(console.log.calledWith('Got topic policy:', policyMock));
6466
});
6567
it('should require topicName', function () {
6668
var sample = getSample();
67-
sample.program.getTopicPolicy(undefined, function (err, policy) {
68-
assert(err);
69-
assert(err.message = '"topicName" is required!');
70-
assert.equal(policy, undefined);
71-
});
69+
var callback = sinon.stub();
70+
71+
sample.program.getTopicPolicy(undefined, callback);
72+
73+
assert(callback.firstCall.args[0]);
74+
assert(callback.firstCall.args[0].message = '"topicName" is required!');
75+
assert.equal(callback.firstCall.args[1], undefined);
7276
});
7377
it('should handle error', function () {
7478
var sample = getSample();
7579
var error = new Error('error');
80+
var callback = sinon.stub();
7681
sample.mocks.topic.iam.getPolicy.callsArgWith(0, error);
77-
sample.program.getTopicPolicy(topicName, function (err) {
78-
assert(err);
79-
assert(err.message === 'error');
80-
});
82+
83+
sample.program.getTopicPolicy(topicName, callback);
84+
85+
assert(callback.firstCall.args[0]);
86+
assert(callback.firstCall.args[0].message === 'error');
8187
});
8288
});
8389

8490
describe('getSubscriptionPolicy', function () {
8591
it('should get a subscription\'s policy', function () {
8692
var sample = getSample();
87-
sample.program.getSubscriptionPolicy(subscriptionName, function (err, policy) {
88-
assert.ifError(err);
89-
assert.equal(policy, policyMock);
90-
assert(console.log.calledWith('Got subscription policy:', policyMock));
91-
});
93+
var callback = sinon.stub();
94+
95+
sample.program.getSubscriptionPolicy(subscriptionName, callback);
96+
97+
assert.ifError(callback.firstCall.args[0]);
98+
assert.equal(callback.firstCall.args[1], policyMock);
99+
assert(console.log.calledWith('Got subscription policy:', policyMock));
92100
});
93101
it('should require subscriptionName', function () {
94102
var sample = getSample();
95-
sample.program.getSubscriptionPolicy(undefined, function (err, policy) {
96-
assert(err);
97-
assert(err.message = '"subscriptionName" is required!');
98-
assert.equal(policy, undefined);
99-
});
103+
var callback = sinon.stub();
104+
105+
sample.program.getSubscriptionPolicy(undefined, callback);
106+
107+
assert(callback.firstCall.args[0]);
108+
assert(callback.firstCall.args[0].message = '"subscriptionName" is required!');
109+
assert.equal(callback.firstCall.args[1], undefined);
100110
});
101111
it('should handle error', function () {
102112
var sample = getSample();
103113
var error = new Error('error');
114+
var callback = sinon.stub();
104115
sample.mocks.subscription.iam.getPolicy.callsArgWith(0, error);
105-
sample.program.getSubscriptionPolicy(subscriptionName, function (err) {
106-
assert(err);
107-
assert(err.message === 'error');
108-
});
116+
117+
sample.program.getSubscriptionPolicy(subscriptionName, callback);
118+
119+
assert(callback.firstCall.args[0]);
120+
assert(callback.firstCall.args[0].message === 'error');
109121
});
110122
});
111123

112124
describe('setTopicPolicy', function () {
113125
it('should set a topic\'s policy', function () {
114126
var sample = getSample();
115-
sample.program.setTopicPolicy(topicName, function (err, policy) {
116-
assert.ifError(err);
117-
assert.equal(policy, policyMock);
118-
assert(console.log.calledWith('Updated policy for topic: %s', topicName));
119-
});
127+
var callback = sinon.stub();
128+
129+
sample.program.setTopicPolicy(topicName, callback);
130+
131+
assert.ifError(callback.firstCall.args[0]);
132+
assert.equal(callback.firstCall.args[1], policyMock);
133+
assert(console.log.calledWith('Updated policy for topic: %s', topicName));
120134
});
121135
it('should require topicName', function () {
122136
var sample = getSample();
123-
sample.program.setTopicPolicy(undefined, function (err, policy) {
124-
assert(err);
125-
assert(err.message = '"topicName" is required!');
126-
assert.equal(policy, undefined);
127-
});
137+
var callback = sinon.stub();
138+
139+
sample.program.setTopicPolicy(undefined, callback);
140+
141+
assert(callback.firstCall.args[0]);
142+
assert(callback.firstCall.args[0].message = '"topicName" is required!');
143+
assert.equal(callback.firstCall.args[1], undefined);
128144
});
129145
it('should handle error', function () {
130146
var sample = getSample();
131147
var error = new Error('error');
148+
var callback = sinon.stub();
132149
sample.mocks.topic.iam.setPolicy.callsArgWith(1, error);
133-
sample.program.setTopicPolicy(topicName, function (err) {
134-
assert(err);
135-
assert(err.message === 'error');
136-
});
150+
151+
sample.program.setTopicPolicy(topicName, callback);
152+
153+
assert(callback.firstCall.args[0]);
154+
assert(callback.firstCall.args[0].message === 'error');
137155
});
138156
});
139157

140158
describe('setSubscriptionPolicy', function () {
141159
it('should set a subscription\'s policy', function () {
142160
var sample = getSample();
143-
sample.program.setSubscriptionPolicy(subscriptionName, function (err, policy) {
144-
assert.ifError(err);
145-
assert.equal(policy, policyMock);
146-
assert(console.log.calledWith('Updated policy for subscription: %s', subscriptionName));
147-
});
161+
var callback = sinon.stub();
162+
163+
sample.program.setSubscriptionPolicy(subscriptionName, callback);
164+
165+
assert.ifError(callback.firstCall.args[0]);
166+
assert.equal(callback.firstCall.args[1], policyMock);
167+
assert(console.log.calledWith('Updated policy for subscription: %s', subscriptionName));
148168
});
149169
it('should require subscriptionName', function () {
150170
var sample = getSample();
151-
sample.program.setSubscriptionPolicy(undefined, function (err, policy) {
152-
assert(err);
153-
assert(err.message = '"subscriptionName" is required!');
154-
assert.equal(policy, undefined);
155-
});
171+
var callback = sinon.stub();
172+
173+
sample.program.setSubscriptionPolicy(undefined, callback);
174+
175+
assert(callback.firstCall.args[0]);
176+
assert(callback.firstCall.args[0].message = '"subscriptionName" is required!');
177+
assert.equal(callback.firstCall.args[1], undefined);
156178
});
157179
it('should handle error', function () {
158180
var sample = getSample();
159181
var error = new Error('error');
182+
var callback = sinon.stub();
160183
sample.mocks.subscription.iam.setPolicy.callsArgWith(1, error);
161-
sample.program.setSubscriptionPolicy(subscriptionName, function (err) {
162-
assert(err);
163-
assert(err.message === 'error');
164-
});
184+
185+
sample.program.setSubscriptionPolicy(subscriptionName, callback);
186+
187+
assert(callback.firstCall.args[0]);
188+
assert(callback.firstCall.args[0].message === 'error');
165189
});
166190
});
167191

168192
describe('testTopicPermissions', function () {
169193
it('should test a topic\'s permissions', function () {
170194
var sample = getSample();
171-
sample.program.testTopicPermissions(topicName, function (err, permissions) {
172-
assert.ifError(err);
173-
assert.equal(permissions, permissionsMock);
174-
assert(console.log.calledWith('Tested permissions for topic: %s', topicName));
175-
});
195+
var callback = sinon.stub();
196+
197+
sample.program.testTopicPermissions(topicName, callback);
198+
199+
assert.ifError(callback.firstCall.args[0]);
200+
assert.equal(callback.firstCall.args[1], permissionsMock);
201+
assert(console.log.calledWith('Tested permissions for topic: %s', topicName));
176202
});
177203
it('should require topicName', function () {
178204
var sample = getSample();
179-
sample.program.testTopicPermissions(undefined, function (err, policy) {
180-
assert(err);
181-
assert(err.message = '"topicName" is required!');
182-
assert.equal(policy, undefined);
183-
});
205+
var callback = sinon.stub();
206+
207+
sample.program.testTopicPermissions(undefined, callback);
208+
209+
assert(callback.firstCall.args[0]);
210+
assert(callback.firstCall.args[0].message = '"topicName" is required!');
211+
assert.equal(callback.firstCall.args[1], undefined);
184212
});
185213
it('should handle error', function () {
186214
var sample = getSample();
187215
var error = new Error('error');
216+
var callback = sinon.stub();
188217
sample.mocks.topic.iam.testPermissions.callsArgWith(1, error);
189-
sample.program.testTopicPermissions(topicName, function (err, permissions) {
190-
assert(err);
191-
assert(err.message === 'error');
192-
assert.equal(permissions, undefined);
193-
});
218+
219+
sample.program.testTopicPermissions(topicName, callback);
220+
221+
assert(callback.firstCall.args[0]);
222+
assert(callback.firstCall.args[0].message === 'error');
223+
assert.equal(callback.firstCall.args[1], undefined);
194224
});
195225
});
196226

197227
describe('testSubscriptionPermissions', function () {
198228
it('should tests a subscription\'s permissions', function () {
199229
var sample = getSample();
200-
sample.program.testSubscriptionPermissions(subscriptionName, function (err, permissions) {
201-
assert.ifError(err);
202-
assert.equal(permissions, permissionsMock);
203-
assert(console.log.calledWith('Tested permissions for subscription: %s', subscriptionName));
204-
});
230+
var callback = sinon.stub();
231+
232+
sample.program.testSubscriptionPermissions(subscriptionName, callback);
233+
234+
assert.ifError(callback.firstCall.args[0]);
235+
assert.equal(callback.firstCall.args[1], permissionsMock);
236+
assert(console.log.calledWith('Tested permissions for subscription: %s', subscriptionName));
205237
});
206238
it('should require subscriptionName', function () {
207239
var sample = getSample();
208-
sample.program.testSubscriptionPermissions(undefined, function (err, policy) {
209-
assert(err);
210-
assert(err.message = '"subscriptionName" is required!');
211-
assert.equal(policy, undefined);
212-
});
240+
var callback = sinon.stub();
241+
242+
sample.program.testSubscriptionPermissions(undefined, callback);
243+
244+
assert(callback.firstCall.args[0]);
245+
assert(callback.firstCall.args[0].message = '"subscriptionName" is required!');
246+
assert.equal(callback.firstCall.args[1], undefined);
213247
});
214248
it('should handle error', function () {
215249
var sample = getSample();
216250
var error = new Error('error');
251+
var callback = sinon.stub();
217252
sample.mocks.subscription.iam.testPermissions.callsArgWith(1, error);
218-
sample.program.testSubscriptionPermissions(subscriptionName, function (err, permissions) {
219-
assert(err);
220-
assert(err.message === 'error');
221-
assert.equal(permissions, undefined);
222-
});
253+
254+
sample.program.testSubscriptionPermissions(subscriptionName, callback);
255+
256+
assert(callback.firstCall.args[0]);
257+
assert(callback.firstCall.args[0].message === 'error');
258+
assert.equal(callback.firstCall.args[1], undefined);
223259
});
224260
});
225261

0 commit comments

Comments
 (0)