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

@@ -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();