Reorganizing spy matcher specs.
This commit is contained in:
parent
752b91f118
commit
b0326d3078
|
@ -471,7 +471,7 @@ describe("jasmine.Matchers", function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("spy matchers (wasCalled, wasNotCalled, wasCalledWith)", function() {
|
describe("spy matchers È", function() {
|
||||||
var TestClass;
|
var TestClass;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
TestClass = {
|
TestClass = {
|
||||||
|
@ -481,30 +481,25 @@ describe("jasmine.Matchers", function() {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw an exception when wasCalled and wasNotCalled are invoked with the wrong number of arguments", function() {
|
describe("wasCalled", function() {
|
||||||
expect(function() {
|
it("should pass iff the spy was called", function() {
|
||||||
match(TestClass.normalFunction).wasCalled("unwanted argument");
|
expect(match(TestClass.spyFunction).wasCalled()).toEqual(false);
|
||||||
}).toThrow('wasCalled does not take arguments, use wasCalledWith');
|
|
||||||
|
|
||||||
expect(function() {
|
TestClass.spyFunction();
|
||||||
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
|
expect(match(TestClass.spyFunction).wasCalled()).toEqual(true);
|
||||||
}).toThrow('wasNotCalled does not take arguments');
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('with non-spies', function() {
|
it("should throw an exception when invoked with any arguments", function() {
|
||||||
it('should always show an error', function () {
|
expect(function() {
|
||||||
|
match(TestClass.normalFunction).wasCalled("unwanted argument");
|
||||||
|
}).toThrow('wasCalled does not take arguments, use wasCalledWith');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an exception when invoked on a non-spy', function () {
|
||||||
expect(function() {
|
expect(function() {
|
||||||
match(TestClass.normalFunction).wasCalled();
|
match(TestClass.normalFunction).wasCalled();
|
||||||
}).toThrow('Expected a spy, but got Function.');
|
}).toThrow('Expected a spy, but got Function.');
|
||||||
|
|
||||||
expect(function() {
|
|
||||||
match(TestClass.normalFunction).wasNotCalled();
|
|
||||||
}).toThrow('Expected a spy, but got Function.');
|
|
||||||
|
|
||||||
expect(function() {
|
|
||||||
match(TestClass.normalFunction).wasCalledWith();
|
|
||||||
}).toThrow('Expected a spy, but got Function.');
|
|
||||||
|
|
||||||
expect(function() {
|
expect(function() {
|
||||||
match(undefined).wasCalled();
|
match(undefined).wasCalled();
|
||||||
}).toThrow('Expected a spy, but got undefined.');
|
}).toThrow('Expected a spy, but got undefined.');
|
||||||
|
@ -515,80 +510,84 @@ describe("jasmine.Matchers", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with spies', function () {
|
describe("wasNotCalled", function() {
|
||||||
describe("wasCalled", function() {
|
it("should pass iff the spy was not called", function() {
|
||||||
it("should pass iff the spy was called", function() {
|
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(true);
|
||||||
expect(match(TestClass.spyFunction).wasCalled()).toEqual(false);
|
|
||||||
|
|
||||||
TestClass.spyFunction();
|
|
||||||
expect(match(TestClass.spyFunction).wasCalled()).toEqual(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
TestClass.spyFunction();
|
||||||
|
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("wasNotCalled", function() {
|
it("should throw an exception when invoked with any arguments", function() {
|
||||||
it("should pass iff the spy was not called", function() {
|
expect(function() {
|
||||||
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(true);
|
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
|
||||||
|
}).toThrow('wasNotCalled does not take arguments');
|
||||||
TestClass.spyFunction();
|
|
||||||
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("wasCalledWith", function() {
|
it('should throw an exception when invoked on a non-spy', function () {
|
||||||
it('wasCalledWith should return true if it was called with the expected args', function() {
|
expect(function() {
|
||||||
TestClass.spyFunction('a', 'b', 'c');
|
match(TestClass.normalFunction).wasNotCalled();
|
||||||
expect(match(TestClass.spyFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
|
}).toThrow('Expected a spy, but got Function.');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("wasCalledWith", function() {
|
||||||
|
it('wasCalledWith 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);
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
||||||
|
expect(result.passed()).toEqual(false);
|
||||||
|
expect(result.expected).toEqual(['c', 'b', 'a']);
|
||||||
|
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
|
||||||
|
});
|
||||||
|
|
||||||
|
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.wasCalledWith('a', 'b', 'c')).toEqual(true);
|
||||||
|
expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true);
|
||||||
|
expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an exception when invoked on a non-spy', function () {
|
||||||
|
expect(function() {
|
||||||
|
match(TestClass.normalFunction).wasCalledWith();
|
||||||
|
}).toThrow('Expected a spy, but got Function.');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("to build an ExpectationResult", function () {
|
||||||
|
beforeEach(function() {
|
||||||
|
var currentSuite;
|
||||||
|
var spec;
|
||||||
|
currentSuite = env.describe('default current suite', function() {
|
||||||
|
spec = env.it();
|
||||||
|
}, spec);
|
||||||
|
TestClass = { someFunction: function(a, b) {
|
||||||
|
} };
|
||||||
|
spec.spyOn(TestClass, 'someFunction');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if it was not called with the expected args', function() {
|
it("should should handle the case of a spy", function() {
|
||||||
TestClass.spyFunction('a', 'b', 'c');
|
TestClass.someFunction('a', 'c');
|
||||||
var expected = match(TestClass.spyFunction);
|
var matcher = match(TestClass.someFunction);
|
||||||
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
|
matcher.wasCalledWith('a', 'b');
|
||||||
|
|
||||||
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
||||||
|
expect(result.matcherName).toEqual("wasCalledWith");
|
||||||
expect(result.passed()).toEqual(false);
|
expect(result.passed()).toEqual(false);
|
||||||
expect(result.expected).toEqual(['c', 'b', 'a']);
|
expect(result.message).toMatch("['a', 'b']");
|
||||||
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
|
expect(result.message).toMatch("['a', 'c']");
|
||||||
});
|
expect(result.actual).toEqual(TestClass.someFunction);
|
||||||
|
expect(result.expected).toEqual(['a','b']);
|
||||||
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.wasCalledWith('a', 'b', 'c')).toEqual(true);
|
|
||||||
expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true);
|
|
||||||
expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("wasCalledWith to build an ExpectationResult", function () {
|
|
||||||
var TestClass;
|
|
||||||
beforeEach(function() {
|
|
||||||
var currentSuite;
|
|
||||||
var spec;
|
|
||||||
currentSuite = env.describe('default current suite', function() {
|
|
||||||
spec = env.it();
|
|
||||||
}, spec);
|
|
||||||
TestClass = { someFunction: function(a, b) {
|
|
||||||
} };
|
|
||||||
spec.spyOn(TestClass, 'someFunction');
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should should handle the case of a spy", function() {
|
|
||||||
TestClass.someFunction('a', 'c');
|
|
||||||
var matcher = match(TestClass.someFunction);
|
|
||||||
matcher.wasCalledWith('a', 'b');
|
|
||||||
|
|
||||||
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.actual).toEqual(TestClass.someFunction);
|
|
||||||
expect(result.expected).toEqual(['a','b']);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue