Permalink
Browse files

Prettier mocking syntax

  • Loading branch information...
1 parent 3a5a263 commit af0efc7d6605f00ebfcda9a29531754da5c3bb0c @bbyars committed Jun 5, 2011
Showing with 101 additions and 31 deletions.
  1. +60 −0 server/test/testExtensions.js
  2. +41 −31 server/test/unit/protocols/http/stubbing.test.js
@@ -88,6 +88,54 @@ var api = {
}
};
+function mock() {
+ var wasCalled = false,
+ actualArguments = [],
+ message = '';
+
+ var stubFunction = function () {
+ wasCalled = true;
+ actualArguments = Array.prototype.slice.call(arguments);
+ };
+
+ var setMessage = function (expected, actual) {
+ message = '\nExpected call with ' + expected;
+ if (wasCalled) {
+ message += '\nActual called with ' + actual;
+ }
+ else {
+ message += '\nNever called';
+ }
+ };
+
+ stubFunction.wasCalled = function () {
+ return wasCalled;
+ };
+
+ stubFunction.wasCalledWith = function () {
+ var args = Array.prototype.slice.call(arguments),
+ expected = JSON.stringify(args),
+ actual = JSON.stringify(actualArguments);
+
+ setMessage(expected, actual);
+ return wasCalled &&
+ JSON.stringify(actualArguments) === JSON.stringify(args);
+ };
+
+ stubFunction.message = function () {
+ return message;
+ };
+
+ return stubFunction;
+}
+
+function withArgs() {
+ var expected = Array.prototype.slice.call(arguments);
+ return function (test, mock) {
+ test.ok(mock.wasCalledWith.apply(null, expected), mock.message());
+ };
+}
+
var addCustomAsserts = function (test) {
test.jsonEquals = function (actual, expected, message) {
var json = function (obj) {
@@ -108,6 +156,16 @@ var addCustomAsserts = function (test) {
test.done();
});
};
+
+ test.wasCalled = function (mock) {
+ if (arguments.length === 1) {
+ test.ok(mock.wasCalled(), 'Expected mock call, none received.');
+ }
+ else {
+ // assume withArgs result
+ arguments[1](test, mock);
+ }
+ };
};
nodeunitTypes.test = function () {
@@ -120,3 +178,5 @@ exports.http = web;
exports.api = api;
exports.controlServerURL = controlServerURL;
exports.adminPort = adminPort;
+exports.mock = mock;
+exports.withArgs = withArgs;
@@ -1,33 +1,10 @@
'use strict';
-require('testExtensions');
-
var testCase = require('nodeunit').testCase,
createRepository = require('repository').create,
- stubs = require('protocols/http/stubbing');
-
-function mock() {
- var wasCalled = false,
- actualArguments = [];
-
- var stubFunction = function () {
- wasCalled = true;
- actualArguments = Array.prototype.slice.call(arguments);
- };
-
- stubFunction.wasCalled = function () {
- return wasCalled;
- };
-
- stubFunction.wasCalledWith = function () {
- var args = Array.prototype.slice.call(arguments);
-
- return wasCalled &&
- JSON.stringify(actualArguments) === JSON.stringify(args);
- };
-
- return stubFunction;
-}
+ stubs = require('protocols/http/stubbing'),
+ mock = require('testExtensions').mock,
+ withArgs = require('testExtensions').withArgs;
exports['HTTP Stubbing'] = testCase({
setUp: function (callback) {
@@ -37,17 +14,50 @@ exports['HTTP Stubbing'] = testCase({
write: mock(),
end: mock()
};
+ this.stubber = stubs.create();
callback();
},
'should have reasonable defaults': function (test) {
- var stubber = stubs.create();
+ this.stubber.middleware(this.request, this.response);
+
+ test.wasCalled(this.response.writeHead, withArgs(200, { 'Connection': 'close' }));
+ test.wasCalled(this.response.write, withArgs(''));
+ test.wasCalled(this.response.end);
+ test.done();
+ }/*,
+
+ 'should return stub if path matches': function (test) {
+ this.request.url = '/test';
+ this.stubber.addStub({
+ path: '/test',
+ response: {
+ statusCode: 400,
+ headers: { 'Content-Type': 'text/plain' },
+ body: 'Testing 1..2..3..'
+ }
+ });
+
+ this.stubber.middleware(this.request, this.response);
+
+ test.ok(this.response.writeHead.wasCalledWith(400, {
+ 'Connection': 'close',
+ 'Content-Type': 'text/plain'
+ }));
+ test.ok(this.response.write.wasCalledWith('Testing 1..2..3..'));
+ test.done();
+ },
+
+ 'should not return stub if path does not match': function (test) {
+ this.request.url = '/';
+ this.stubber.addStub({
+ path: '/test',
+ response: { body: 'Testing 1..2..3..' }
+ });
- stubber.middleware(this.request, this.response);
+ this.stubber.middleware(this.request, this.response);
- test.ok(this.response.writeHead.wasCalledWith(200, { 'Connection': 'close' }));
test.ok(this.response.write.wasCalledWith(''));
- test.ok(this.response.end.wasCalled());
test.done();
- }
+ }*/
});

0 comments on commit af0efc7

Please sign in to comment.