Fix some broken spy examples (thanks to Tom Crayford).

This commit is contained in:
Christian Williams 2010-09-12 12:59:31 -07:00
parent 1d51d224c7
commit 7dfc04a555
1 changed files with 31 additions and 14 deletions

View File

@ -293,31 +293,48 @@ Here are a few examples:
var Klass = function () {
};
var Klass.prototype.method = function (arg) {
Klass.staticMethod = function (arg) {
return arg;
};
var Klass.prototype.methodWithCallback = function (callback) {
Klass.prototype.method = function (arg) {
return arg;
};
Klass.prototype.methodWithCallback = function (callback) {
return callback('foo');
};
...
it('should spy on Klass#method') {
spyOn(Klass, 'method');
Klass.method('foo argument');
describe("spy behavior", function() {
it('should spy on a static method of Klass', function() {
spyOn(Klass, 'staticMethod');
Klass.staticMethod('foo argument');
expect(Klass.method).toHaveBeenCalledWith('foo argument');
expect(Klass.staticMethod).toHaveBeenCalledWith('foo argument');
});
it('should spy on an instance method of a Klass', function() {
var obj = new Klass();
spyOn(obj, 'method');
obj.method('foo argument');
expect(obj.method).toHaveBeenCalledWith('foo argument');
var obj2 = new Klass();
spyOn(obj2, 'method');
expect(obj2.method).not.toHaveBeenCalled();
});
it('should spy on Klass#methodWithCallback', function() {
var callback = jasmine.createSpy();
new Klass().methodWithCallback(callback);
expect(callback).toHaveBeenCalledWith('foo');
});
});
it('should spy on Klass#methodWithCallback') {
var callback = jasmine.createSpy();
Klass.methodWithCallback(callback);
expect(callback).toHaveBeenCalledWith('foo');
});
Spies can be very useful for testing AJAX or other asynchronous behaviors that take callbacks by faking the method firing an async call.
var Klass = function () {