deprecating wasCalled, wasCalledWith, wasNotCalled, wasNotCalledWith and adding toHaveBeenCalled, and toHaveBeenCalledWith

This commit is contained in:
Lee Byrd & Christian Williams 2010-06-24 10:34:03 -07:00
parent 6a0e452788
commit 2939aff80c
12 changed files with 116 additions and 64 deletions

View File

@ -291,14 +291,14 @@ Here are a few examples:
spyOn(Klass, 'method');
Klass.method('foo argument');
expect(Klass.method).wasCalledWith('foo argument');
expect(Klass.method).toHaveBeenCalledWith('foo argument');
});
it('should spy on Klass#methodWithCallback') {
var callback = Jasmine.createSpy();
Klass.methodWithCallback(callback);
expect(callback).wasCalledWith('foo');
expect(callback).toHaveBeenCalledWith('foo');
});
@ -318,23 +318,25 @@ Spies can be very useful for testing AJAX or other asynchronous behaviors that t
var callback = Jasmine.createSpy();
Klass.asyncMethod(callback);
expect(callback).wasNotCalled();
expect(callback).not.toHaveBeenCalled();
var someResponseData = 'foo';
Klass.asyncMethod.mostRecentCall.args[0](someResponseData);
expect(callback).wasCalledWith(someResponseData);
expect(callback).toHaveBeenCalledWith(someResponseData);
});
There are spy-specfic matchers that are very handy.
`expect(x).wasCalled()` passes if `x` is a spy and was called
`expect(x).toHaveBeenCalled()` passes if `x` is a spy and was called
`expect(x).wasCalledWith(arguments)` passes if `x` is a spy and was called with the specified arguments
`expect(x).toHaveBeenCalledWith(arguments)` passes if `x` is a spy and was called with the specified arguments
`expect(x).wasNotCalled()` passes if `x` is a spy and was not called
`expect(x).not.toHaveBeenCalled()` passes if `x` is a spy and was not called
`expect(x).wasNotCalledWith(arguments)` passes if `x` is a spy and was not called with the specified arguments
`expect(x).not.toHaveBeenCalledWith(arguments)` passes if `x` is a spy and was not called with the specified arguments
The old matchers `wasCalled`, `wasNotCalled`, `wasCalledWith`, and `wasNotCalledWith` have been deprecated and will be removed in a future release. Please change your specs to use `toHaveBeenCalled`, `not.toHaveBeenCalled`, `toHaveBeenCalledWith`, and `not.toHaveBeenCalledWith` respectively.
Spies can be trained to respond in a variety of ways when invoked:

View File

@ -172,7 +172,7 @@ jasmine.isDomNode = function(obj) {
*
* @example
* // don't care about which function is passed in, as long as it's a function
* expect(mySpy).wasCalledWith(jasmine.any(Function));
* expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function));
*
* @param {Class} clazz
* @returns matchable object of the type clazz
@ -187,7 +187,8 @@ jasmine.any = function(clazz) {
* Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine
* expectation syntax. Spies can be checked if they were called or not and what the calling params were.
*
* A Spy has the following mehtod: wasCalled, callCount, mostRecentCall, and argsForCall (see docs)
* A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs).
*
* Spies are torn down at the end of every spec.
*
* Note: Do <b>not</b> call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj.
@ -217,8 +218,8 @@ jasmine.any = function(clazz) {
*
* // mock example
* foo.not(7 == 7);
* expect(foo.not).wasCalled();
* expect(foo.not).wasCalledWith(true);
* expect(foo.not).toHaveBeenCalled();
* expect(foo.not).toHaveBeenCalledWith(true);
*
* @constructor
* @see spyOn, jasmine.createSpy, jasmine.createSpyObj
@ -1224,12 +1225,18 @@ jasmine.Matchers.prototype.toBeFalsy = function() {
return !this.actual;
};
/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
jasmine.Matchers.prototype.wasCalled = function() {
return this.toHaveBeenCalled();
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called.
*/
jasmine.Matchers.prototype.wasCalled = function() {
jasmine.Matchers.prototype.toHaveBeenCalled = function() {
if (arguments.length > 0) {
throw new Error('wasCalled does not take arguments, use wasCalledWith');
throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
}
if (!jasmine.isSpy(this.actual)) {
@ -1245,6 +1252,8 @@ jasmine.Matchers.prototype.wasCalled = function() {
/**
* Matcher that checks to see if the actual, a Jasmine spy, was not called.
*
* @deprecated Use expect(xxx).not.toHaveBeenCalled() instead
*/
jasmine.Matchers.prototype.wasNotCalled = function() {
if (arguments.length > 0) {
@ -1262,13 +1271,18 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
return !this.actual.wasCalled;
};
/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasCalledWith = function() {
return this.toHaveBeenCalledWith.apply(this, arguments);
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
*
* @example
*
*/
jasmine.Matchers.prototype.wasCalledWith = function() {
jasmine.Matchers.prototype.toHaveBeenCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
@ -1284,6 +1298,7 @@ jasmine.Matchers.prototype.wasCalledWith = function() {
return this.env.contains_(this.actual.argsForCall, expectedArgs);
};
/** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasNotCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
@ -2249,7 +2264,6 @@ jasmine.Clock = {
},
uninstallMock: function() {
jasmine.log("uninstall")
jasmine.Clock.assertInstalled();
jasmine.Clock.installed = jasmine.Clock.real;
},
@ -2313,5 +2327,5 @@ jasmine.version_= {
"major": 0,
"minor": 11,
"build": 0,
"revision": 1277317833
"revision": 1277400646
};

View File

@ -11,7 +11,7 @@ describe("base.js", function() {
it("should accept n arguments", function() {
spyOn(jasmine.getEnv().currentSpec, 'log');
jasmine.log(1, 2, 3);
expect(jasmine.getEnv().currentSpec.log).wasCalledWith(1, 2, 3);
expect(jasmine.getEnv().currentSpec.log).toHaveBeenCalledWith(1, 2, 3);
});
});

View File

@ -79,7 +79,7 @@ describe("jasmine.Env", function() {
it("should allow reporters to be registered", function() {
env.addReporter(fakeReporter);
env.reporter.log("message");
expect(fakeReporter.log).wasCalledWith("message");
expect(fakeReporter.log).toHaveBeenCalledWith("message");
});
});

View File

@ -15,7 +15,7 @@ describe("jasmine.Matchers", function() {
function match(value) {
return spec.expect(value);
}
function lastResult() {
return spec.addMatcherResult.mostRecentCall.args[0];
}
@ -555,23 +555,34 @@ describe("jasmine.Matchers", function() {
};
}
describe("wasCalled", function() {
it("should pass iff the spy was called", function() {
expect(match(TestClass.spyFunction).wasCalled()).toEqual(false);
describe("toHaveBeenCalled", function() {
it("should pass if the spy was called", function() {
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toEqual(false);
TestClass.spyFunction();
expect(match(TestClass.spyFunction).wasCalled()).toEqual(true);
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toEqual(true);
});
it("should throw an exception when invoked with any arguments", function() {
expect(function() {
match(TestClass.normalFunction).wasCalled("unwanted argument");
}).toThrow('wasCalled does not take arguments, use wasCalledWith');
match(TestClass.normalFunction).toHaveBeenCalled("unwanted argument");
}).toThrow('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasCalled'));
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalled'));
});
describe("wasCalled", function() {
it("should alias toHaveBeenCalled", function() {
spyOn(TestClass, 'normalFunction');
TestClass.normalFunction();
expect(TestClass.normalFunction).wasCalled();
});
});
describe("wasNotCalled", function() {
it("should pass iff the spy was not called", function() {
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(true);
@ -589,16 +600,16 @@ describe("jasmine.Matchers", function() {
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalled'));
});
describe("wasCalledWith", function() {
it('wasCalledWith should return true if it was called with the expected args', function() {
describe("toHaveBeenCalledWith", function() {
it('toHaveBeenCalledWith should return true if it was called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
expect(match(TestClass.spyFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
expect(match(TestClass.spyFunction).toHaveBeenCalledWith('a', 'b', 'c')).toEqual(true);
});
it('should return false if it was not called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
var expected = match(TestClass.spyFunction);
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toEqual(false);
var result = lastResult();
expect(result.passed()).toEqual(false);
expect(result.expected).toEqual(['c', 'b', 'a']);
@ -609,7 +620,7 @@ describe("jasmine.Matchers", function() {
it('should return false if it was not called', function() {
var expected = match(TestClass.spyFunction);
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toEqual(false);
var result = lastResult();
expect(result.passed()).toEqual(false);
expect(result.expected).toEqual(['c', 'b', 'a']);
@ -621,12 +632,12 @@ describe("jasmine.Matchers", function() {
var expected = match(TestClass.spyFunction);
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
expect(expected.wasCalledWith('a', 'b', 'c')).toEqual(true);
expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true);
expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false);
expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toEqual(true);
expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toEqual(true);
expect(expected.toHaveBeenCalledWith('x', 'y', 'z')).toEqual(false);
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasCalledWith'));
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalledWith'));
describe("to build an ExpectationResult", function () {
beforeEach(function() {
@ -643,10 +654,10 @@ describe("jasmine.Matchers", function() {
it("should should handle the case of a spy", function() {
TestClass.someFunction('a', 'c');
var matcher = match(TestClass.someFunction);
matcher.wasCalledWith('a', 'b');
matcher.toHaveBeenCalledWith('a', 'b');
var result = lastResult();
expect(result.matcherName).toEqual("wasCalledWith");
expect(result.matcherName).toEqual("toHaveBeenCalledWith");
expect(result.passed()).toEqual(false);
expect(result.message).toContain(jasmine.pp(['a', 'b']));
expect(result.message).toContain(jasmine.pp(['a', 'c']));
@ -656,6 +667,16 @@ describe("jasmine.Matchers", function() {
});
});
describe("wasCalledWith", function() {
it("should alias toHaveBeenCalledWith", function() {
spyOn(TestClass, 'normalFunction');
TestClass.normalFunction(123);
expect(TestClass.normalFunction).wasCalledWith(123);
});
});
describe("wasNotCalledWith", function() {
it('should return true if the spy was NOT called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
@ -676,7 +697,7 @@ describe("jasmine.Matchers", function() {
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);

View File

@ -19,12 +19,12 @@ describe("jasmine.MultiReporter", function() {
it("should delegate to any and all subreporters", function() {
multiReporter.reportSpecResults('blah', 'foo');
expect(fakeReporter1.reportSpecResults).wasCalledWith('blah', 'foo');
expect(fakeReporter2.reportSpecResults).wasCalledWith('blah', 'foo');
expect(fakeReporter1.reportSpecResults).toHaveBeenCalledWith('blah', 'foo');
expect(fakeReporter2.reportSpecResults).toHaveBeenCalledWith('blah', 'foo');
});
it("should quietly skip delegating to any subreporters which lack the given method", function() {
multiReporter.reportRunnerStarting('blah', 'foo');
expect(fakeReporter2.reportRunnerStarting).wasCalledWith('blah', 'foo');
expect(fakeReporter2.reportRunnerStarting).toHaveBeenCalledWith('blah', 'foo');
});
});

View File

@ -210,11 +210,11 @@ describe('RunnerTest', function() {
});
env.currentRunner().execute();
expect(fakeReporter.reportRunnerResults).wasNotCalled();
expect(fakeReporter.reportRunnerResults).not.toHaveBeenCalled();
fakeTimer.tick(200);
//This blows up the JSApiReporter.
//expect(fakeReporter.reportRunnerResults).wasCalledWith(env.currentRunner);
expect(fakeReporter.reportRunnerResults).wasCalled();
//expect(fakeReporter.reportRunnerResults).toHaveBeenCalledWith(env.currentRunner);
expect(fakeReporter.reportRunnerResults).toHaveBeenCalled();
expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].results()).toEqual(env.currentRunner().results());
});
});
@ -227,12 +227,12 @@ describe('RunnerTest', function() {
var runner = new jasmine.Runner(env);
runner.arbitraryVariable = 'foo';
spyOn(runner.queue, 'start');
expect(fakeReporter.reportRunnerStarting).wasNotCalled();
expect(fakeReporter.reportRunnerStarting).not.toHaveBeenCalled();
runner.execute();
expect(fakeReporter.reportRunnerStarting).wasCalled();
expect(fakeReporter.reportRunnerStarting).toHaveBeenCalled();
var reportedRunner = fakeReporter.reportRunnerStarting.mostRecentCall.args[0];
expect(reportedRunner.arbitraryVariable).toEqual('foo');
expect(runner.queue.start).wasCalled();
expect(runner.queue.start).toHaveBeenCalled();
});
describe("when suites are nested", function() {

View File

@ -1118,7 +1118,7 @@ describe("jasmine spec running", function () {
disabledSuite.execute();
expect(spy).wasNotCalled();
expect(spy).not.toHaveBeenCalled();
});
it('#explodes should throw an exception when it is called inside a spec', function() {

View File

@ -169,11 +169,11 @@ describe('Spies', function () {
var TestClass = { someFunction: function() {} };
this.spyOn(TestClass, 'someFunction');
expect(TestClass.someFunction).wasNotCalled();
expect(TestClass.someFunction).not.toHaveBeenCalled();
TestClass.someFunction();
expect(TestClass.someFunction).wasCalled();
expect(TestClass.someFunction).toHaveBeenCalled();
TestClass.someFunction.reset();
expect(TestClass.someFunction).wasNotCalled();
expect(TestClass.someFunction).not.toHaveBeenCalled();
expect(TestClass.someFunction.callCount).toEqual(0);
});

View File

@ -17,7 +17,7 @@ describe('WaitsForBlock', function () {
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
expect(onComplete).wasNotCalled();
block.execute(onComplete);
expect(onComplete).wasCalled();
expect(onComplete).toHaveBeenCalled();
});
it('latchFunction should run in same scope as spec', function () {
@ -36,8 +36,8 @@ describe('WaitsForBlock', function () {
spyOn(spec, 'fail');
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
block.execute(onComplete);
expect(spec.fail).wasCalledWith('some error');
expect(onComplete).wasCalled();
expect(spec.fail).toHaveBeenCalledWith('some error');
expect(onComplete).toHaveBeenCalled();
});
describe("if latchFunction returns false", function() {
@ -69,7 +69,7 @@ describe('WaitsForBlock', function () {
expect(onComplete).wasNotCalled();
latchFunction.andReturn(true);
fakeTimer.tick(100);
expect(onComplete).wasCalled();
expect(onComplete).toHaveBeenCalled();
});
it('spec should fail with the passed message if the timeout is reached (and not call onComplete)', function () {
@ -78,7 +78,7 @@ describe('WaitsForBlock', function () {
block.execute(onComplete);
expect(spec.fail).wasNotCalled();
fakeTimer.tick(timeout);
expect(spec.fail).wasCalled();
expect(spec.fail).toHaveBeenCalled();
var failMessage = spec.fail.mostRecentCall.args[0].message;
expect(failMessage).toMatch(message);
expect(onComplete).wasNotCalled();

View File

@ -162,12 +162,18 @@ jasmine.Matchers.prototype.toBeFalsy = function() {
return !this.actual;
};
/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
jasmine.Matchers.prototype.wasCalled = function() {
return this.toHaveBeenCalled();
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called.
*/
jasmine.Matchers.prototype.wasCalled = function() {
jasmine.Matchers.prototype.toHaveBeenCalled = function() {
if (arguments.length > 0) {
throw new Error('wasCalled does not take arguments, use wasCalledWith');
throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
}
if (!jasmine.isSpy(this.actual)) {
@ -183,6 +189,8 @@ jasmine.Matchers.prototype.wasCalled = function() {
/**
* Matcher that checks to see if the actual, a Jasmine spy, was not called.
*
* @deprecated Use expect(xxx).not.toHaveBeenCalled() instead
*/
jasmine.Matchers.prototype.wasNotCalled = function() {
if (arguments.length > 0) {
@ -200,13 +208,18 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
return !this.actual.wasCalled;
};
/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasCalledWith = function() {
return this.toHaveBeenCalledWith.apply(this, arguments);
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
*
* @example
*
*/
jasmine.Matchers.prototype.wasCalledWith = function() {
jasmine.Matchers.prototype.toHaveBeenCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
@ -222,6 +235,7 @@ jasmine.Matchers.prototype.wasCalledWith = function() {
return this.env.contains_(this.actual.argsForCall, expectedArgs);
};
/** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasNotCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
if (!jasmine.isSpy(this.actual)) {

View File

@ -172,7 +172,7 @@ jasmine.isDomNode = function(obj) {
*
* @example
* // don't care about which function is passed in, as long as it's a function
* expect(mySpy).wasCalledWith(jasmine.any(Function));
* expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function));
*
* @param {Class} clazz
* @returns matchable object of the type clazz
@ -187,7 +187,8 @@ jasmine.any = function(clazz) {
* Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine
* expectation syntax. Spies can be checked if they were called or not and what the calling params were.
*
* A Spy has the following mehtod: wasCalled, callCount, mostRecentCall, and argsForCall (see docs)
* A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs).
*
* Spies are torn down at the end of every spec.
*
* Note: Do <b>not</b> call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj.
@ -217,8 +218,8 @@ jasmine.any = function(clazz) {
*
* // mock example
* foo.not(7 == 7);
* expect(foo.not).wasCalled();
* expect(foo.not).wasCalledWith(true);
* expect(foo.not).toHaveBeenCalled();
* expect(foo.not).toHaveBeenCalledWith(true);
*
* @constructor
* @see spyOn, jasmine.createSpy, jasmine.createSpyObj