timeAsyncMethodTests.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. (function() {
  2. var SandboxedModule, chai, expect, modulePath, path, should, sinon;
  3. require('coffee-script');
  4. chai = require('chai');
  5. should = chai.should();
  6. expect = chai.expect;
  7. path = require('path');
  8. modulePath = path.join(__dirname, '../../../timeAsyncMethod.coffee');
  9. SandboxedModule = require('sandboxed-module');
  10. sinon = require("sinon");
  11. describe('timeAsyncMethod', function() {
  12. beforeEach(function() {
  13. this.Timer = {
  14. done: sinon.stub()
  15. };
  16. this.TimerConstructor = sinon.stub().returns(this.Timer);
  17. this.metrics = {
  18. Timer: this.TimerConstructor
  19. };
  20. this.timeAsyncMethod = SandboxedModule.require(modulePath, {
  21. requires: {
  22. './metrics': this.metrics
  23. }
  24. });
  25. return this.testObject = {
  26. nextNumber: function(n, callback) {
  27. if (callback == null) {
  28. callback = function(err, result) {};
  29. }
  30. return setTimeout(function() {
  31. return callback(null, n + 1);
  32. }, 100);
  33. }
  34. };
  35. });
  36. it('should have the testObject behave correctly before wrapping', function(done) {
  37. return this.testObject.nextNumber(2, function(err, result) {
  38. expect(err).to.not.exist;
  39. expect(result).to.equal(3);
  40. return done();
  41. });
  42. });
  43. it('should wrap method without error', function(done) {
  44. this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
  45. return done();
  46. });
  47. return it('should work', function(done) {
  48. this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
  49. return this.testObject.nextNumber(2, (function(_this) {
  50. return function(err, result) {
  51. expect(err).to.not.exist;
  52. expect(result).to.equal(3);
  53. expect(_this.TimerConstructor.callCount).to.equal(1);
  54. expect(_this.Timer.done.callCount).to.equal(1);
  55. return done();
  56. };
  57. })(this));
  58. });
  59. });
  60. }).call(this);