Fix message bug with wasCalledWith. Throw on bad arguments to createSpyObj

This commit is contained in:
Josh Susser 2010-02-24 19:27:43 -08:00
parent aef78b1ef1
commit 5e3eb884ca
6 changed files with 98 additions and 28 deletions

View File

@ -7,7 +7,7 @@ gems:
- name: json
version: 1.1.9
- name: selenium-rc
version: 2.1.0
version: 2.2.0
- name: rack
version: 1.0.0
- name: thin
@ -17,7 +17,7 @@ gems:
- name: rspec
version: 1.2.9
- name: selenium-client
version: 1.2.17
version: 1.2.18
- name: rest-client
version: 1.0.3
- name: saucelabs-adapter

View File

@ -350,6 +350,9 @@ jasmine.isSpy = function(putativeSpy) {
* @param {Array} methodNames array of names of methods to make spies
*/
jasmine.createSpyObj = function(baseName, methodNames) {
if (!jasmine.isArray_(methodNames) || methodNames.length == 0) {
throw new Error('createSpyObj requires a non-empty array of method names to create spies for');
}
var obj = {};
for (var i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = jasmine.createSpy(baseName + '.' + methodNames[i]);
@ -1204,31 +1207,32 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
*
*/
jasmine.Matchers.prototype.wasCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
this.message = function() {
if (this.actual.callCount == 0) {
return "Expected spy to have been called with " + jasmine.pp(arguments) + " but it was never called.";
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.";
} else {
return "Expected spy to have been called with " + jasmine.pp(arguments) + " but was called with " + jasmine.pp(this.actual.argsForCall);
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall);
}
};
return this.env.contains_(this.actual.argsForCall, jasmine.util.argsToArray(arguments));
return this.env.contains_(this.actual.argsForCall, expectedArgs);
};
jasmine.Matchers.prototype.wasNotCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
this.message = function() {
return "Expected spy not to have been called with " + jasmine.pp(arguments) + " but it was";
return "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was";
};
return !this.env.contains_(this.actual.argsForCall, jasmine.util.argsToArray(arguments));
return !this.env.contains_(this.actual.argsForCall, expectedArgs);
};
/**
@ -2257,5 +2261,5 @@ jasmine.version_= {
"major": 0,
"minor": 10,
"build": 0,
"revision": 1264648722
"revision": 1267068340
};

View File

@ -545,6 +545,18 @@ describe("jasmine.Matchers", function() {
expect(result.passed()).toEqual(false);
expect(result.expected).toEqual(['c', 'b', 'a']);
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
expect(result.message).toContain(jasmine.pp(result.expected));
expect(result.message).toContain(jasmine.pp(result.actual.mostRecentCall.args));
});
it('should return false if it was not called', function() {
var expected = match(TestClass.spyFunction);
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.passed()).toEqual(false);
expect(result.expected).toEqual(['c', 'b', 'a']);
expect(result.actual.argsForCall).toEqual([]);
expect(result.message).toContain(jasmine.pp(result.expected));
});
it('should allow matches across multiple calls', function() {
@ -578,12 +590,48 @@ describe("jasmine.Matchers", function() {
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.matcherName).toEqual("wasCalledWith");
expect(result.passed()).toEqual(false);
expect(result.message).toMatch("['a', 'b']");
expect(result.message).toMatch("['a', 'c']");
expect(result.message).toContain(jasmine.pp(['a', 'b']));
expect(result.message).toContain(jasmine.pp(['a', 'c']));
expect(result.actual).toEqual(TestClass.someFunction);
expect(result.expected).toEqual(['a','b']);
});
});
});
describe("wasNotCalledWith", function() {
it('should return true if the spy was NOT called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
expect(match(TestClass.spyFunction).wasNotCalledWith('c', 'b', 'a')).toEqual(true);
});
it('should return false if it WAS called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
var expected = match(TestClass.spyFunction);
expect(expected.wasNotCalledWith('a', 'b', 'c')).toEqual(false);
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
expect(result.passed()).toEqual(false);
expect(result.expected).toEqual(['a', 'b', 'c']);
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
expect(result.message).toContain(jasmine.pp(result.expected));
});
it('should return true if it was not called', function() {
var expected = match(TestClass.spyFunction);
expect(expected.wasNotCalledWith('c', 'b', 'a')).toEqual(true);
});
it('should allow matches across multiple calls', function() {
var expected = match(TestClass.spyFunction);
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
expect(expected.wasNotCalledWith('a', 'b', 'c')).toEqual(false);
expect(expected.wasNotCalledWith('d', 'e', 'f')).toEqual(false);
expect(expected.wasNotCalledWith('x', 'y', 'z')).toEqual(true);
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalledWith'));
});
});
});

View File

@ -177,6 +177,7 @@ describe('Spies', function () {
expect(TestClass.someFunction.callCount).toEqual(0);
});
describe("createSpyObj", function() {
it("should create an object with a bunch of spy methods when you call jasmine.createSpyObj()", function() {
var spyObj = jasmine.createSpyObj('BaseName', ['method1', 'method2']);
expect(spyObj).toEqual({ method1: jasmine.any(Function), method2: jasmine.any(Function)});
@ -184,4 +185,17 @@ describe('Spies', function () {
expect(spyObj.method2.identity).toEqual('BaseName.method2');
});
it("should throw if you do not pass an array argument", function() {
expect(function() {
jasmine.createSpyObj('BaseName');
}).toThrow('createSpyObj requires a non-empty array of method names to create spies for');
});
it("should throw if you pass an empty array argument", function() {
expect(function() {
jasmine.createSpyObj('BaseName');
}).toThrow('createSpyObj requires a non-empty array of method names to create spies for');
});
});
});

View File

@ -206,31 +206,32 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
*
*/
jasmine.Matchers.prototype.wasCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
this.message = function() {
if (this.actual.callCount == 0) {
return "Expected spy to have been called with " + jasmine.pp(arguments) + " but it was never called.";
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.";
} else {
return "Expected spy to have been called with " + jasmine.pp(arguments) + " but was called with " + jasmine.pp(this.actual.argsForCall);
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall);
}
};
return this.env.contains_(this.actual.argsForCall, jasmine.util.argsToArray(arguments));
return this.env.contains_(this.actual.argsForCall, expectedArgs);
};
jasmine.Matchers.prototype.wasNotCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
this.message = function() {
return "Expected spy not to have been called with " + jasmine.pp(arguments) + " but it was";
return "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was";
};
return !this.env.contains_(this.actual.argsForCall, jasmine.util.argsToArray(arguments));
return !this.env.contains_(this.actual.argsForCall, expectedArgs);
};
/**

View File

@ -350,6 +350,9 @@ jasmine.isSpy = function(putativeSpy) {
* @param {Array} methodNames array of names of methods to make spies
*/
jasmine.createSpyObj = function(baseName, methodNames) {
if (!jasmine.isArray_(methodNames) || methodNames.length == 0) {
throw new Error('createSpyObj requires a non-empty array of method names to create spies for');
}
var obj = {};
for (var i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = jasmine.createSpy(baseName + '.' + methodNames[i]);