From 52dc521570f7ac2befe03bfab9163123fba67c89 Mon Sep 17 00:00:00 2001 From: Ryan Kelly Date: Thu, 11 Feb 2016 11:06:10 +1100 Subject: [PATCH] refactor(tests): Use a shared helper function for mocking out logging. --- test/local/password_check_test.js | 6 +-- test/local/push_tests.js | 80 +++++++++++++++---------------- test/local/statsd_tests.js | 4 +- test/mocks.js | 27 +++++++++++ 4 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 test/mocks.js diff --git a/test/local/password_check_test.js b/test/local/password_check_test.js index 1ea58ffdd..47ada8633 100644 --- a/test/local/password_check_test.js +++ b/test/local/password_check_test.js @@ -4,9 +4,9 @@ var P = require('../../lib/promise') var test = require('../ptaptest') -var MockLog = { info: function () { } } +var mockLog = require('../mocks').mockLog() var config = { lockoutEnabled: true } -var Password = require('../../lib/crypto/password')(MockLog, config) +var Password = require('../../lib/crypto/password')(mockLog, config) var triggersLockout = false var MockCustoms = { @@ -28,7 +28,7 @@ var MockDB = { } } -var checkPassword = require('../../lib/routes/utils/password_check')(MockLog, config, Password, MockCustoms, MockDB) +var checkPassword = require('../../lib/routes/utils/password_check')(mockLog, config, Password, MockCustoms, MockDB) test( 'password check with correct password', diff --git a/test/local/push_tests.js b/test/local/push_tests.js index 816c01cdd..a67359199 100644 --- a/test/local/push_tests.js +++ b/test/local/push_tests.js @@ -8,15 +8,8 @@ var extend = require('util')._extend var test = tap.test var P = require('../../lib/promise') +var mockLog = require('../mocks').mockLog var mockUid = new Buffer('foo') -var mockLog = { - error: function () { - }, - increment: function () { - }, - trace: function () { - } -} var mockDbEmpty = { devices: function () { @@ -52,12 +45,13 @@ var mockDbResult = { test( 'notifyUpdate does not throw on empty device result', function (t) { - var thisMockLog = extend({}, mockLog) - thisMockLog.increment = function (log) { - if (log === 'push.success') { - t.fail('must not call push.success') + var thisMockLog = mockLog({ + increment: function (name) { + if (name === 'push.success') { + t.fail('must not call push.success') + } } - } + }) try { var push = require('../../lib/push')(thisMockLog, mockDbEmpty) @@ -76,18 +70,19 @@ test( 'notifyUpdate sends notifications', function (t) { var successCalled = 0 - var thisMockLog = extend({}, mockLog) - thisMockLog.increment = function (log) { - if (log === 'push.success') { - // notification sent - successCalled++ - } + var thisMockLog = mockLog({ + increment: function (log) { + if (log === 'push.success') { + // notification sent + successCalled++ + } - if (successCalled === 2) { - // we should send 2 notifications for 2 devices - t.end() + if (successCalled === 2) { + // we should send 2 notifications for 2 devices + t.end() + } } - } + }) var mocks = { request: { @@ -114,13 +109,14 @@ test( } } - var thisMockLog = extend({}, mockLog) - thisMockLog.increment = function (log) { - if (log === 'push.no_push_callback') { - // device had no push callback - t.end() + var thisMockLog = mockLog({ + increment: function (log) { + if (log === 'push.no_push_callback') { + // device had no push callback + t.end() + } } - } + }) var push = require('../../lib/push')(thisMockLog, mockDbNoCallback) push.notifyUpdate(mockUid) @@ -141,13 +137,14 @@ test( } } - var thisMockLog = extend({}, mockLog) - thisMockLog.increment = function (log) { - if (log === 'push.failed') { - // request failed - t.end() + var thisMockLog = mockLog({ + increment: function (log) { + if (log === 'push.failed') { + // request failed + t.end() + } } - } + }) var mocks = { request: { @@ -179,13 +176,14 @@ test( } } - var thisMockLog = extend({}, mockLog) - thisMockLog.increment = function (log) { - if (log === 'push.reset_settings') { - // request failed - t.end() + var thisMockLog = mockLog({ + increment: function (log) { + if (log === 'push.reset_settings') { + // request failed + t.end() + } } - } + }) var mocks = { request: { diff --git a/test/local/statsd_tests.js b/test/local/statsd_tests.js index 2397d2b35..e39018aa8 100644 --- a/test/local/statsd_tests.js +++ b/test/local/statsd_tests.js @@ -6,9 +6,7 @@ var tap = require('tap') var test = tap.test var StatsDCollector = require('../../lib/metrics/statsd') -var mockLog = { - error: function () {} -} +var mockLog = require('../mocks').mockLog() test( 'statsd init failure cases', diff --git a/test/mocks.js b/test/mocks.js new file mode 100644 index 000000000..cc3214506 --- /dev/null +++ b/test/mocks.js @@ -0,0 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * Shared helpers for mocking things out in the tests. + */ + + +// A logging mock that doesn't capture anything. +// You can pass in an object of custom logging methods +// if you need to e.g. capture messages. + + +var mockLog = function(methods) { + var log = {} + var noop = function () {} + methods = methods || {} + ;['trace', 'increment', 'info', 'error'].forEach(function(name) { + log[name] = (methods[name] || noop).bind(log) + }) + return log +} + +module.exports = { + mockLog: mockLog +}