Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Extended spy to include the ability to check a spied function was cal…

…led with specified arguments
  • Loading branch information...
commit c5d1858e46b552346e008546468acdba680a25a9 1 parent 00a7011
authored
10  example/test-spyon.js
@@ -79,6 +79,16 @@ exports['test_spyon_count_correct'] = function (test, assert) {
79 79
   test.finish();
80 80
 };
81 81
 
  82
+exports['test_spyon_with_arguments_correct'] = function (test, assert) {
  83
+  var foo, arr;
  84
+  foo = new Foo();
  85
+  arr = ['a', 'b', null];
  86
+  test.spy.on('setBar', foo);
  87
+  foo.setBar(arr);
  88
+  assert.ok(test.spy.called('setBar').with(arr));
  89
+  test.spy.clear('setBar', foo);
  90
+  test.finish();
  91
+};
82 92
 
83 93
 exports['test_spyon_accepts_function'] = function (test, assert) {
84 94
   var foo, func, set;
47  lib/common.js
@@ -551,11 +551,11 @@ exports.registerCustomAssertionFunctions = registerCustomAssertionFunctions;
551 551
  */
552 552
 function SpyOn (){
553 553
   /**
554  
-   * This tracks the number of times a function has been fired.
555  
-   * @param {Object.<number>}
  554
+   * This tracks the arguments a function has been called with.
  555
+   * @param {Object.<Array>}
556 556
    * @private
557 557
    */
558  
-  this._callCount = {};
  558
+  this._calls = {};
559 559
   /**
560 560
    * This tracks the function to call
561 561
    * @param {Object.<Function>}
@@ -577,12 +577,12 @@ SpyOn.prototype.on = function (funcName, context, optFunc) {
577 577
   } else {
578 578
     func = context[funcName];
579 579
   }
580  
-  if (this._callCount.hasOwnProperty(funcName)) {
  580
+  if (this._calls.hasOwnProperty(funcName)) {
581 581
     throw "Function already being tracked.";
582 582
   }
583 583
   this.reset(funcName);
584 584
   wrapper = (function () {
585  
-    this._callCount[funcName] += 1;
  585
+    this._calls[funcName].push(Array.prototype.slice.call(arguments));
586 586
     return func.apply(context, arguments);
587 587
   }).bind(this);
588 588
   context[funcName] = wrapper;
@@ -601,7 +601,7 @@ SpyOn.prototype.clear = function (funcName, context, optFunc) {
601 601
   } else {
602 602
     context[funcName] = this._funcMap[funcName];
603 603
   }
604  
-  delete this._callCount[funcName];
  604
+  delete this._calls[funcName];
605 605
   delete this._funcMap[funcName];
606 606
   return this;
607 607
 };
@@ -611,7 +611,7 @@ SpyOn.prototype.clear = function (funcName, context, optFunc) {
611 611
  * @param {string} funcName The name of the function.
612 612
  */
613 613
 SpyOn.prototype.reset = function (funcName) {
614  
-  this._callCount[funcName] = 0;
  614
+  this._calls[funcName] = [];
615 615
 };
616 616
 
617 617
 /**
@@ -619,6 +619,35 @@ SpyOn.prototype.reset = function (funcName) {
619 619
  * @param {string} funcName
620 620
  */
621 621
 SpyOn.prototype.called = function (funcName) {
622  
-  return this._callCount[funcName];
623  
-};
  622
+  var calls = this._calls[funcName];
  623
+
  624
+  // checks the actual args match the expected args
  625
+  var checkArgsMatch = function (actualArgs, expectedArgs) {
  626
+    var i;
  627
+    if (actualArgs.length !== expectedArgs.length) {
  628
+      return false;
  629
+    }
  630
+    for (i = 0; i < expectedArgs.length; i++) {
  631
+      if (actualArgs[i] !== expectedArgs[i]) {
  632
+        return false;
  633
+      }
  634
+    }
  635
+    return true;
  636
+  };
624 637
 
  638
+  return {
  639
+    valueOf: function () {
  640
+      return calls.length;
  641
+    },
  642
+    with: function () {
  643
+      var i, j, match;
  644
+      var args = Array.prototype.slice.call(arguments);
  645
+      for (i = 0; i < calls.length; i++) {
  646
+        if (checkArgsMatch(calls[i], args)) {
  647
+          return true;
  648
+        }
  649
+      }
  650
+      return false;
  651
+    }
  652
+  }
  653
+};

0 notes on commit c5d1858

Please sign in to comment.
Something went wrong with that request. Please try again.