diff --git a/bigquery/test/queries.test.js b/bigquery/test/queries.test.js index 83051d1dd0..ab2d2188ee 100644 --- a/bigquery/test/queries.test.js +++ b/bigquery/test/queries.test.js @@ -63,35 +63,39 @@ describe('bigquery:query', function () { it('should return results', function () { var example = getSample(); - example.program.syncQuery(query, - function (err, data) { - assert.ifError(err); - assert(example.mocks.bigquery.query.called); - assert.deepEqual(data, example.mocks.natality); - assert(console.log.calledWith( - 'SyncQuery: found %d rows!', - data.length - )); - } - ); + var callback = sinon.stub(); + + example.program.syncQuery(query, callback); + + assert.ifError(callback.firstCall.args[0]); + assert(example.mocks.bigquery.query.called); + assert.deepEqual(callback.firstCall.args[1], example.mocks.natality); + assert(console.log.calledWith( + 'SyncQuery: found %d rows!', + callback.firstCall.args[1].length + )); }); it('should require a query', function () { var example = getSample(); - example.program.syncQuery(null, function (err, data) { - assert.deepEqual(err, Error('"query" is required!')); - assert.equal(data, undefined); - }); + var callback = sinon.stub(); + + example.program.syncQuery(null, callback); + + assert.deepEqual(callback.firstCall.args[0], new Error('"query" is required!')); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var error = new Error('error'); var example = getSample(); + var callback = sinon.stub(); example.mocks.bigquery.query = sinon.stub().callsArgWith(1, error); - example.program.syncQuery(query, function (err, data) { - assert.deepEqual(err, error); - assert.equal(data, undefined); - }); + + example.program.syncQuery(query, callback); + + assert.deepEqual(callback.firstCall.args[0], error); + assert.equal(callback.firstCall.args[1], undefined); }); }); @@ -100,106 +104,119 @@ describe('bigquery:query', function () { it('should submit a job', function () { var example = getSample(); - example.program.asyncQuery(query, - function (err, job) { - assert.ifError(err); - assert(example.mocks.bigquery.startQuery.called); - assert.deepEqual(example.mocks.job, job); - assert(console.log.calledWith( - 'AsyncQuery: submitted job %s!', example.jobId - )); - } - ); + var callback = sinon.stub(); + + example.program.asyncQuery(query, callback); + + assert.ifError(callback.firstCall.args[0]); + assert(example.mocks.bigquery.startQuery.called); + assert.deepEqual(callback.firstCall.args[1], example.mocks.job); + assert(console.log.calledWith( + 'AsyncQuery: submitted job %s!', example.jobId + )); }); it('should require a query', function () { var example = getSample(); - example.program.asyncQuery(null, function (err, job) { - assert.deepEqual(err, Error('"query" is required!')); - assert.equal(job, undefined); - }); + var callback = sinon.stub(); + + example.program.asyncQuery(null, callback); + + assert.deepEqual(callback.firstCall.args[0], new Error('"query" is required!')); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var error = new Error('error'); var example = getSample(); + var callback = sinon.stub(); example.mocks.bigquery.startQuery = sinon.stub().callsArgWith(1, error); - example.program.asyncQuery(query, function (err, job) { - assert.deepEqual(err, error); - assert.equal(job, undefined); - }); + + example.program.asyncQuery(query, callback); + + assert.deepEqual(callback.firstCall.args[0], error); + assert.equal(callback.firstCall.args[1], undefined); }); }); describe('asyncPoll', function () { it('should get the results of a job given its ID', function () { var example = getSample(); + var callback = sinon.stub(); example.mocks.bigquery.job = sinon.stub().returns(example.mocks.job); - example.program.asyncPoll(example.jobId, - function (err, rows) { - assert.ifError(err); - assert(example.mocks.job.getQueryResults.called); - assert(console.log.calledWith( - 'AsyncQuery: polled job %s; got %d rows!', - example.jobId, - example.mocks.natality.length - )); - } - ); + + example.program.asyncPoll(example.jobId, callback); + + assert.ifError(callback.firstCall.args[0]); + assert(example.mocks.job.getQueryResults.called); + assert(console.log.calledWith( + 'AsyncQuery: polled job %s; got %d rows!', + example.jobId, + example.mocks.natality.length + )); }); it('should report the status of a job', function () { var example = getSample(); - example.program.asyncPoll(example.jobId, function (err, rows) { - assert.ifError(err); - assert(example.mocks.job.getMetadata.called); - assert(console.log.calledWith( - 'Job status: %s', - example.mocks.metadata.status.state - )); - }); + var callback = sinon.stub(); + + example.program.asyncPoll(example.jobId, callback); + + assert.ifError(callback.firstCall.args[0]); + assert(example.mocks.job.getMetadata.called); + assert(console.log.calledWith( + 'Job status: %s', + example.mocks.metadata.status.state + )); }); it('should check whether a job is finished', function () { var example = getSample(); + var callback = sinon.stub(); var pendingState = { status: { state: 'PENDING' } }; example.mocks.job.getMetadata = sinon.stub().callsArgWith(0, null, pendingState); - example.program.asyncPoll(example.jobId, function (err, rows) { - assert.deepEqual(err, Error('Job %s is not done', example.jobId)); - assert(console.log.calledWith('Job status: %s', pendingState.status.state)); - assert(example.mocks.job.getMetadata.called); - assert.equal(example.mocks.job.getQueryResults.called, false); - assert.equal(rows, undefined); - }); + + example.program.asyncPoll(example.jobId, callback); + + assert.deepEqual(callback.firstCall.args[0], Error('Job %s is not done', example.jobId)); + assert(console.log.calledWith('Job status: %s', pendingState.status.state)); + assert(example.mocks.job.getMetadata.called); + assert.equal(example.mocks.job.getQueryResults.called, false); + assert.equal(callback.firstCall.args[1], undefined); var doneState = { status: { state: 'DONE' } }; example.mocks.job.getMetadata = sinon.stub().callsArgWith(0, null, doneState); - example.program.asyncPoll(example.jobId, function (err, rows) { - assert.ifError(err); - assert(console.log.calledWith('Job status: %s', doneState.status.state)); - assert(example.mocks.job.getMetadata.called); - assert(example.mocks.job.getQueryResults.called); - assert.equal(rows, example.mocks.natality); - }); + + example.program.asyncPoll(example.jobId, callback); + + assert.ifError(callback.secondCall.args[0]); + assert(console.log.calledWith('Job status: %s', doneState.status.state)); + assert(example.mocks.job.getMetadata.called); + assert(example.mocks.job.getQueryResults.called); + assert.equal(callback.secondCall.args[1], example.mocks.natality); }); it('should require a job ID', function () { var example = getSample(); - example.program.asyncPoll(null, function (err, rows) { - assert.deepEqual(err, Error('"jobId" is required!')); - assert.equal(rows, undefined); - }); + var callback = sinon.stub(); + + example.program.asyncPoll(null, callback); + + assert.deepEqual(callback.firstCall.args[0], Error('"jobId" is required!')); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var error = new Error('error'); var example = getSample(); + var callback = sinon.stub(); example.mocks.job.getQueryResults = sinon.stub().callsArgWith(0, error); - example.program.asyncPoll(example.jobId, function (err, rows) { - assert.deepEqual(err, error); - assert.equal(rows, undefined); - }); + + example.program.asyncPoll(example.jobId, callback); + + assert.deepEqual(callback.firstCall.args[0], error); + assert.equal(callback.firstCall.args[1], undefined); }); }); diff --git a/pubsub/test/iam.test.js b/pubsub/test/iam.test.js index c8a6308dac..8c87510edc 100644 --- a/pubsub/test/iam.test.js +++ b/pubsub/test/iam.test.js @@ -56,170 +56,206 @@ describe('pubsub:iam', function () { describe('getTopicPolicy', function () { it('should get a topic\'s policy', function () { var sample = getSample(); - sample.program.getTopicPolicy(topicName, function (err, policy) { - assert.ifError(err); - assert.equal(policy, policyMock); - assert(console.log.calledWith('Got topic policy:', policyMock)); - }); + var callback = sinon.stub(); + + sample.program.getTopicPolicy(topicName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.equal(callback.firstCall.args[1], policyMock); + assert(console.log.calledWith('Got topic policy:', policyMock)); }); it('should require topicName', function () { var sample = getSample(); - sample.program.getTopicPolicy(undefined, function (err, policy) { - assert(err); - assert(err.message = '"topicName" is required!'); - assert.equal(policy, undefined); - }); + var callback = sinon.stub(); + + sample.program.getTopicPolicy(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"topicName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.topic.iam.getPolicy.callsArgWith(0, error); - sample.program.getTopicPolicy(topicName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.getTopicPolicy(topicName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('getSubscriptionPolicy', function () { it('should get a subscription\'s policy', function () { var sample = getSample(); - sample.program.getSubscriptionPolicy(subscriptionName, function (err, policy) { - assert.ifError(err); - assert.equal(policy, policyMock); - assert(console.log.calledWith('Got subscription policy:', policyMock)); - }); + var callback = sinon.stub(); + + sample.program.getSubscriptionPolicy(subscriptionName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.equal(callback.firstCall.args[1], policyMock); + assert(console.log.calledWith('Got subscription policy:', policyMock)); }); it('should require subscriptionName', function () { var sample = getSample(); - sample.program.getSubscriptionPolicy(undefined, function (err, policy) { - assert(err); - assert(err.message = '"subscriptionName" is required!'); - assert.equal(policy, undefined); - }); + var callback = sinon.stub(); + + sample.program.getSubscriptionPolicy(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"subscriptionName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.subscription.iam.getPolicy.callsArgWith(0, error); - sample.program.getSubscriptionPolicy(subscriptionName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.getSubscriptionPolicy(subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('setTopicPolicy', function () { it('should set a topic\'s policy', function () { var sample = getSample(); - sample.program.setTopicPolicy(topicName, function (err, policy) { - assert.ifError(err); - assert.equal(policy, policyMock); - assert(console.log.calledWith('Updated policy for topic: %s', topicName)); - }); + var callback = sinon.stub(); + + sample.program.setTopicPolicy(topicName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.equal(callback.firstCall.args[1], policyMock); + assert(console.log.calledWith('Updated policy for topic: %s', topicName)); }); it('should require topicName', function () { var sample = getSample(); - sample.program.setTopicPolicy(undefined, function (err, policy) { - assert(err); - assert(err.message = '"topicName" is required!'); - assert.equal(policy, undefined); - }); + var callback = sinon.stub(); + + sample.program.setTopicPolicy(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"topicName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.topic.iam.setPolicy.callsArgWith(1, error); - sample.program.setTopicPolicy(topicName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.setTopicPolicy(topicName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('setSubscriptionPolicy', function () { it('should set a subscription\'s policy', function () { var sample = getSample(); - sample.program.setSubscriptionPolicy(subscriptionName, function (err, policy) { - assert.ifError(err); - assert.equal(policy, policyMock); - assert(console.log.calledWith('Updated policy for subscription: %s', subscriptionName)); - }); + var callback = sinon.stub(); + + sample.program.setSubscriptionPolicy(subscriptionName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.equal(callback.firstCall.args[1], policyMock); + assert(console.log.calledWith('Updated policy for subscription: %s', subscriptionName)); }); it('should require subscriptionName', function () { var sample = getSample(); - sample.program.setSubscriptionPolicy(undefined, function (err, policy) { - assert(err); - assert(err.message = '"subscriptionName" is required!'); - assert.equal(policy, undefined); - }); + var callback = sinon.stub(); + + sample.program.setSubscriptionPolicy(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"subscriptionName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.subscription.iam.setPolicy.callsArgWith(1, error); - sample.program.setSubscriptionPolicy(subscriptionName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.setSubscriptionPolicy(subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('testTopicPermissions', function () { it('should test a topic\'s permissions', function () { var sample = getSample(); - sample.program.testTopicPermissions(topicName, function (err, permissions) { - assert.ifError(err); - assert.equal(permissions, permissionsMock); - assert(console.log.calledWith('Tested permissions for topic: %s', topicName)); - }); + var callback = sinon.stub(); + + sample.program.testTopicPermissions(topicName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.equal(callback.firstCall.args[1], permissionsMock); + assert(console.log.calledWith('Tested permissions for topic: %s', topicName)); }); it('should require topicName', function () { var sample = getSample(); - sample.program.testTopicPermissions(undefined, function (err, policy) { - assert(err); - assert(err.message = '"topicName" is required!'); - assert.equal(policy, undefined); - }); + var callback = sinon.stub(); + + sample.program.testTopicPermissions(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"topicName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.topic.iam.testPermissions.callsArgWith(1, error); - sample.program.testTopicPermissions(topicName, function (err, permissions) { - assert(err); - assert(err.message === 'error'); - assert.equal(permissions, undefined); - }); + + sample.program.testTopicPermissions(topicName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); + assert.equal(callback.firstCall.args[1], undefined); }); }); describe('testSubscriptionPermissions', function () { it('should tests a subscription\'s permissions', function () { var sample = getSample(); - sample.program.testSubscriptionPermissions(subscriptionName, function (err, permissions) { - assert.ifError(err); - assert.equal(permissions, permissionsMock); - assert(console.log.calledWith('Tested permissions for subscription: %s', subscriptionName)); - }); + var callback = sinon.stub(); + + sample.program.testSubscriptionPermissions(subscriptionName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.equal(callback.firstCall.args[1], permissionsMock); + assert(console.log.calledWith('Tested permissions for subscription: %s', subscriptionName)); }); it('should require subscriptionName', function () { var sample = getSample(); - sample.program.testSubscriptionPermissions(undefined, function (err, policy) { - assert(err); - assert(err.message = '"subscriptionName" is required!'); - assert.equal(policy, undefined); - }); + var callback = sinon.stub(); + + sample.program.testSubscriptionPermissions(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"subscriptionName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.subscription.iam.testPermissions.callsArgWith(1, error); - sample.program.testSubscriptionPermissions(subscriptionName, function (err, permissions) { - assert(err); - assert(err.message === 'error'); - assert.equal(permissions, undefined); - }); + + sample.program.testSubscriptionPermissions(subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); + assert.equal(callback.firstCall.args[1], undefined); }); }); diff --git a/pubsub/test/subscriptions.test.js b/pubsub/test/subscriptions.test.js index 5860616562..a460a8e85e 100644 --- a/pubsub/test/subscriptions.test.js +++ b/pubsub/test/subscriptions.test.js @@ -67,131 +67,159 @@ describe('pubsub:subscriptions', function () { describe('create', function () { it('should create a subscription', function () { var sample = getSample(); - sample.program.createSubscription(topicName, subscriptionName, function (err, subscription) { - assert.ifError(err); - assert.strictEqual(subscription, sample.mocks.subscription); - assert(console.log.calledWith('Created subscription %s to topic %s', subscriptionName, topicName)); - }); + var callback = sinon.stub(); + + sample.program.createSubscription(topicName, subscriptionName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.strictEqual(callback.firstCall.args[1], sample.mocks.subscription); + assert(console.log.calledWith('Created subscription %s to topic %s', subscriptionName, topicName)); }); it('should require topicName', function () { var sample = getSample(); - sample.program.createSubscription(undefined, undefined, function (err, subscription) { - assert(err); - assert(err.message = '"topicName" is required!'); - assert.equal(subscription, undefined); - }); + var callback = sinon.stub(); + + sample.program.createSubscription(undefined, undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"topicName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should require subscriptionName', function () { var sample = getSample(); - sample.program.createSubscription(topicName, undefined, function (err, subscription) { - assert(err); - assert(err.message = '"subscriptionName" is required!'); - assert.equal(subscription, undefined); - }); + var callback = sinon.stub(); + + sample.program.createSubscription(topicName, undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"subscriptionName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.pubsub.subscribe.callsArgWith(3, error); - sample.program.createSubscription(topicName, subscriptionName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.createSubscription(topicName, subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('delete', function () { it('should delete a subscription', function () { var sample = getSample(); - sample.program.deleteSubscription(subscriptionName, function (err) { - assert.ifError(err); - assert(console.log.calledWith('Deleted subscription: %s', subscriptionName)); - }); + var callback = sinon.stub(); + + sample.program.deleteSubscription(subscriptionName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert(console.log.calledWith('Deleted subscription: %s', subscriptionName)); }); it('should require subscriptionName', function () { var sample = getSample(); - sample.program.deleteSubscription(undefined, function (err, subscription) { - assert(err); - assert(err.message = '"subscriptionName" is required!'); - assert.equal(subscription, undefined); - }); + var callback = sinon.stub(); + + sample.program.deleteSubscription(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"subscriptionName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.subscription.delete.callsArgWith(0, error); - sample.program.deleteSubscription(subscriptionName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.deleteSubscription(subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('list', function () { it('should list all subscriptions', function () { var sample = getSample(); - sample.program.listSubscriptions(undefined, function (err, subscriptions) { - assert.ifError(err); - assert.strictEqual(subscriptions, sample.mocks.subscriptions); - assert(console.log.calledWith('Found %d subscriptions!', subscriptions.length)); - }); + var callback = sinon.stub(); + + sample.program.listSubscriptions(undefined, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.strictEqual(callback.firstCall.args[1], sample.mocks.subscriptions); + assert(console.log.calledWith('Found %d subscriptions!', callback.firstCall.args[1].length)); }); it('should list all subscriptions of a topic', function () { var sample = getSample(); - sample.program.listSubscriptions(topicName, function (err, subscriptions) { - assert.ifError(err); - assert.strictEqual(subscriptions, sample.mocks.subscriptions); - assert(console.log.calledWith('Found %d subscriptions!', subscriptions.length)); - }); + var callback = sinon.stub(); + + sample.program.listSubscriptions(topicName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.strictEqual(callback.firstCall.args[1], sample.mocks.subscriptions); + assert(console.log.calledWith('Found %d subscriptions!', callback.firstCall.args[1].length)); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.pubsub.getSubscriptions.callsArgWith(1, error); - sample.program.listSubscriptions(undefined, function (err, subscriptions) { - assert(err); - assert(err.message === 'error'); - assert.equal(subscriptions, undefined); - }); + + sample.program.listSubscriptions(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); + assert.equal(callback.firstCall.args[1], undefined); }); }); describe('pull', function () { it('should pull messages', function () { var sample = getSample(); - sample.program.pullMessages(subscriptionName, function (err, messages) { - assert.ifError(err); - assert.strictEqual(messages, sample.mocks.messages); - assert(console.log.calledWith('Pulled %d messages!', messages.length)); - assert(console.log.calledWith('Acked %d messages!', messages.length)); - }); + var callback = sinon.stub(); + + sample.program.pullMessages(subscriptionName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.strictEqual(callback.firstCall.args[1], sample.mocks.messages); + assert(console.log.calledWith('Pulled %d messages!', callback.firstCall.args[1].length)); + assert(console.log.calledWith('Acked %d messages!', callback.firstCall.args[1].length)); }); it('should require subscriptionName', function () { var sample = getSample(); - sample.program.pullMessages(undefined, function (err, subscription) { - assert(err); - assert(err.message = '"subscriptionName" is required!'); - assert.equal(subscription, undefined); - }); + var callback = sinon.stub(); + + sample.program.pullMessages(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"subscriptionName" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle pull error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.subscription.pull.callsArgWith(1, error); - sample.program.pullMessages(subscriptionName, function (err, messages) { - assert(err); - assert(err.message === 'error'); - assert.equal(messages, undefined); - }); + + sample.program.pullMessages(subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle ack error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.subscription.ack.callsArgWith(1, error); - sample.program.pullMessages(subscriptionName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.pullMessages(subscriptionName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); diff --git a/pubsub/test/topics.test.js b/pubsub/test/topics.test.js index 06461013a3..ee58b78c4d 100644 --- a/pubsub/test/topics.test.js +++ b/pubsub/test/topics.test.js @@ -51,121 +51,147 @@ describe('pubsub:topics', function () { describe('create', function () { it('should create a topic', function () { var sample = getSample(); - sample.program.createTopic(topicName, function (err, topic) { - assert.ifError(err); - assert.strictEqual(topic, sample.mocks.topic); - assert(console.log.calledWith('Created topic: %s', topicName)); - }); + var callback = sinon.stub(); + + sample.program.createTopic(topicName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert.strictEqual(callback.firstCall.args[1], sample.mocks.topic); + assert(console.log.calledWith('Created topic: %s', topicName)); }); it('should require name', function () { var sample = getSample(); - sample.program.createTopic(undefined, function (err, topic) { - assert(err); - assert(err.message = '"name" is required!'); - assert.equal(topic, undefined); - }); + var callback = sinon.stub(); + + sample.program.createTopic(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"name" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.topic.get.callsArgWith(1, error); - sample.program.createTopic(topicName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.createTopic(topicName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('delete', function () { it('should delete a topic', function () { var sample = getSample(); - sample.program.deleteTopic(topicName, function (err) { - assert.ifError(err); - assert(console.log.calledWith('Deleted topic: %s', topicName)); - }); + var callback = sinon.stub(); + + sample.program.deleteTopic(topicName, callback); + + assert.ifError(callback.firstCall.args[0]); + assert(console.log.calledWith('Deleted topic: %s', topicName)); }); it('should require name', function () { var sample = getSample(); - sample.program.deleteTopic(undefined, function (err, topic) { - assert(err); - assert(err.message = '"name" is required!'); - assert.equal(topic, undefined); - }); + var callback = sinon.stub(); + + sample.program.deleteTopic(undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"name" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.topic.delete.callsArgWith(0, error); - sample.program.deleteTopic(topicName, function (err) { - assert(err); - assert(err.message === 'error'); - }); + + sample.program.deleteTopic(topicName, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); }); }); describe('publish', function () { it('should publish a message', function () { var sample = getSample(); - sample.program.publishMessage(topicName, '{"data":"hello world"}', function (err, messageIds) { - assert.ifError(err); - assert.deepEqual(messageIds, [1]); - assert(console.log.calledWith('Published %d messages!', messageIds.length)); - }); + var callback = sinon.stub(); + + sample.program.publishMessage(topicName, '{"data":"hello world"}', callback); + + assert.ifError(callback.firstCall.args[0]); + assert.deepEqual(callback.firstCall.args[1], [1]); + assert(console.log.calledWith('Published %d messages!', callback.firstCall.args[1].length)); }); it('should require name', function () { var sample = getSample(); - sample.program.publishMessage(undefined, undefined, function (err, messageIds) { - assert(err); - assert(err.message = '"name" is required!'); - assert.equal(messageIds, undefined); - }); + var callback = sinon.stub(); + + sample.program.publishMessage(undefined, undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"name" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should require message', function () { var sample = getSample(); - sample.program.publishMessage(topicName, undefined, function (err, messageIds) { - assert(err); - assert(err.message = '"message" is required!'); - assert.equal(messageIds, undefined); - }); + var callback = sinon.stub(); + + sample.program.publishMessage(topicName, undefined, callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"message" is required!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should require a valid JSON string', function () { var sample = getSample(); - sample.program.publishMessage(topicName, 'asdf', function (err, messageIds) { - assert(err); - assert(err.message = '"message" must be a valid JSON string!'); - assert.equal(messageIds, undefined); - }); + var callback = sinon.stub(); + + sample.program.publishMessage(topicName, 'asdf', callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message = '"message" must be a valid JSON string!'); + assert.equal(callback.firstCall.args[1], undefined); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.topic.publish.callsArgWith(1, error); - sample.program.publishMessage(topicName, '{"data":"hello world"}', function (err, messageIds) { - assert(err); - assert(err.message === 'error'); - assert.equal(messageIds, undefined); - }); + + sample.program.publishMessage(topicName, '{"data":"hello world"}', callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); + assert.equal(callback.firstCall.args[1], undefined); }); }); describe('list', function () { it('should list topics', function () { var sample = getSample(); - sample.program.listTopics(function (err, topics) { - assert.ifError(err); - assert.strictEqual(topics, sample.mocks.topics); - assert(console.log.calledWith('Found %d topics!', topics.length)); - }); + var callback = sinon.stub(); + + sample.program.listTopics(callback); + + assert.ifError(callback.firstCall.args[0]); + assert.strictEqual(callback.firstCall.args[1], sample.mocks.topics); + assert(console.log.calledWith('Found %d topics!', callback.firstCall.args[1].length)); }); it('should handle error', function () { var sample = getSample(); var error = new Error('error'); + var callback = sinon.stub(); sample.mocks.pubsub.getTopics.callsArgWith(0, error); - sample.program.listTopics(function (err, topics) { - assert(err); - assert(err.message === 'error'); - assert.equal(topics, undefined); - }); + + sample.program.listTopics(callback); + + assert(callback.firstCall.args[0]); + assert(callback.firstCall.args[0].message === 'error'); + assert.equal(callback.firstCall.args[1], undefined); }); });