Skip to content

Commit

Permalink
fixed up the loaclCache mock so that changing the return value worked…
Browse files Browse the repository at this point in the history
… as expected
  • Loading branch information
supernomad committed Jun 21, 2015
1 parent 86e5c96 commit d416ac5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
5 changes: 4 additions & 1 deletion mocks/libs/caching/localCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var typeHelper = require.main.require('libs/helpers/typeHelper'),
cache = {},
returnValue = true;

function setReturnValue(retValue) {
returnValue = retValue;
}
function create(key, val, ttl, callback) {
cache[key] = val;
callback(null, returnValue);
Expand All @@ -21,7 +24,7 @@ function del(key, callback) {
}

module.exports = {
returnValue: returnValue,
setReturnValue: setReturnValue,
"create": create,
"restore": restore,
"update": update,
Expand Down
50 changes: 37 additions & 13 deletions test/routes/chunked-upload-routes-tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/* global describe, it */
/* global describe, it, afterEach */
var should = require('should'),
errorModels = require.main.require('libs/models/errorModels'),
guidHelper = require.main.require('libs/helpers/guidHelper');

describe("chunked-upload-routes.js", function() {

var io_mock = require.main.require('mocks/libs/io'),
cache_mock = require.main.require('mocks/libs/caching/localCache'),
routes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, routePrefix:"/chunked/upload"}),
uploadId = null;

afterEach('reset the cache_mock', function() {
cache_mock.setReturnValue(true);
});

it('should return a route object', function() {
should.exist(routes);
routes.should.be.a.Object;
Expand Down Expand Up @@ -47,7 +52,7 @@ describe("chunked-upload-routes.js", function() {
});

it('should throw a ServerError if the cache fails to store the upload data', function() {
cache_mock.returnValue = false;
cache_mock.setReturnValue(false);
(function() {
routes.post.handler({
body: {
Expand All @@ -61,8 +66,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
cache_mock.returnValue = true;
}).should.throw(errorModels.GenericError);;
});

it('should throw a ValidationError if the request object is considered invalid', function() {
Expand All @@ -74,7 +78,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});
});

Expand Down Expand Up @@ -114,7 +118,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});

it('should throw a MissingCacheItem error if the supplied uploadId does not exist', function() {
Expand All @@ -127,7 +131,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});
});

Expand All @@ -142,6 +146,26 @@ describe("chunked-upload-routes.js", function() {
routes.put.handler.should.be.a.Function;
});

it('should throw a ServerError when the specified uploadId does not exist', function() {
cache_mock.setReturnValue(false);
(function() {
routes.put.handler({
params: {
uploadId: uploadId,
index: 0
},
files: {
testFile: {
path: "random/path/to/nothing"
}
}
}, {
json: function(data) {
}
});
}).should.throw(errorModels.GenericError);
})

it('should upload a file chunk and associate it with the specified upload', function(done) {
routes.put.handler({
params: {
Expand Down Expand Up @@ -196,7 +220,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});

it('should throw a ValidationError if the supplied uploadId is considered invalid', function() {
Expand All @@ -215,7 +239,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});

it('should throw a ValidationError if the supplied index is considered invalid', function() {
Expand All @@ -234,7 +258,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});

it('should throw a MissingCacheItem error if the supplied uploadId does not exist', function() {
Expand All @@ -253,7 +277,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});
});

Expand Down Expand Up @@ -294,7 +318,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});

it('should throw a MissingCacheItem error if the supplied uploadId does not exist', function() {
Expand All @@ -307,7 +331,7 @@ describe("chunked-upload-routes.js", function() {
json: function(data) {
}
});
}).should.throw();
}).should.throw(errorModels.GenericError);;
});
});

Expand Down

0 comments on commit d416ac5

Please sign in to comment.