Permalink
Browse files

Add tests for spyOn and fix issues.

  • Loading branch information...
1 parent b07665c commit ecb56bf833280ba1f86ec34db1aa6327acbe4d4b Bjorn Tipling committed May 11, 2012
Showing with 132 additions and 3 deletions.
  1. +129 −0 example/test-spyon.js
  2. +3 −3 lib/common.js
View
@@ -0,0 +1,129 @@
+/*
+ * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * Cloudkick licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @constructor
+ */
+function Foo () {
+ /**
+ * @param {bool}
+ */
+ this.fooCalled = false;
+ /**
+ * @param {string}
+ */
+ this.bar = "";
+ /**
+ * @param {string}
+ */
+ this.postFix = 'postFix';
+}
+
+Foo.prototype.setFoo = function () {
+ this.fooCalled = true;
+};
+
+/**
+ * @param {string} bar
+ */
+Foo.prototype.setBar = function (bar) {
+ this.bar = bar;
+ return bar + this.postFix;
+};
+
+exports['test_spyon_calls_method'] = function(test, assert) {
+ var foo = new Foo();
+ test.spy.on('setFoo', foo);
+ foo.setFoo();
+ assert.equal(foo.fooCalled, true);
+ test.spy.clear('setFoo', foo);
+ test.finish();
+};
+
+exports['test_spyon_returns_right'] = function(test, assert) {
+ var foo, str;
+ foo = new Foo();
+ str = 'foo';
+ test.spy.on('setBar', foo);
+ assert.equal(foo.setBar(str), str + foo.postFix);
+ test.spy.clear('setBar', foo);
+ test.finish();
+};
+
+exports['test_spyon_count_correct'] = function (test, assert) {
+ var foo = new Foo();
+ test.spy.on('setFoo', foo);
+ foo.setFoo();
+ foo.setFoo();
+ foo.setFoo();
+ foo.setFoo();
+ foo.setFoo();
+ foo.setFoo();
+ foo.setFoo();
+ assert.equal(test.spy.called('setFoo'), 7);
+ test.spy.clear('setFoo', foo);
+ test.finish();
+};
+
+
+exports['test_spyon_accepts_function'] = function (test, assert) {
+ var foo, func, set;
+ foo = new Foo();
+ set = false;
+ func = function () {
+ set = true;
+ }
+ test.spy.on('setFoo', foo, func);
+ foo.setFoo();
+ assert.equal(set, true);
+ test.spy.clear('setFoo', foo);
+ test.finish();
+};
+
+exports['test_spyon_reapplies_original_function'] = function (test, assert) {
+ var foo, func;
+ foo = new Foo();
+ func = foo.setFoo;
+ test.spy.on('setFoo', foo);
+ test.spy.clear('setFoo', foo);
+ assert.equal(foo.setFoo, func);
+ test.finish();
+};
+
+exports['test_spyon_reapplies_given_function'] = function (test, assert) {
+ var foo, func, set;
+ foo = new Foo();
+ set = false;
+ func = function () {
+ set = true;
+ };
+ test.spy.on('setFoo', foo);
+ test.spy.clear('setFoo', foo, func);
+ assert.equal(foo.setFoo, func);
+ test.finish();
+};
+
+exports['test_spyon_reset'] = function(test, assert) {
+ var foo = new Foo();
+ test.spy.on('setFoo', foo);
+ foo.setFoo();
+ test.spy.reset('setFoo');
+ assert.equal(test.spy.called('setFoo'), 0);
+ test.spy.clear('setFoo', foo);
+ test.finish();
+};
+
View
@@ -199,7 +199,7 @@ Test.prototype._getTestObject = function(finishFunc) {
testObj.finish = finishFunc;
testObj.skip = skipFunc;
- testObj.spyOn = new SpyOn();
+ testObj.spy = new SpyOn();
return testObj;
};
@@ -570,7 +570,7 @@ function SpyOn (){
* @param {Function} optFunc The optional function to call in wrapper. If not
* provided, original function will be called.
*/
-SpyOn.prototype.spyOn = function (funcName, context, optFunc) {
+SpyOn.prototype.on = function (funcName, context, optFunc) {
var wrapper, func;
if (optFunc) {
func = optFunc;
@@ -597,7 +597,7 @@ SpyOn.prototype.spyOn = function (funcName, context, optFunc) {
*/
SpyOn.prototype.clear = function (funcName, context, optFunc) {
if (optFunc) {
- context[funcName] = func;
+ context[funcName] = optFunc
} else {
context[funcName] = this._funcMap[funcName];
}

0 comments on commit ecb56bf

Please sign in to comment.