Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
refactor(tests): Use a shared helper function for mocking out logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfk committed Feb 11, 2016
1 parent 8680d22 commit 52dc521
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 47 deletions.
6 changes: 3 additions & 3 deletions test/local/password_check_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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',
Expand Down
80 changes: 39 additions & 41 deletions test/local/push_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
Expand All @@ -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: {
Expand All @@ -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)
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
4 changes: 1 addition & 3 deletions test/local/statsd_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
27 changes: 27 additions & 0 deletions test/mocks.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 52dc521

Please sign in to comment.