|
|
|
|
@@ -1,20 +1,14 @@
|
|
|
|
|
describe("jasmine.Matchers", function() {
|
|
|
|
|
var env;
|
|
|
|
|
|
|
|
|
|
var env, mockSpec;
|
|
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
|
env = new jasmine.Env();
|
|
|
|
|
env.updateInterval = 0;
|
|
|
|
|
mockSpec = jasmine.createSpyObj('spec', ['addMatcherResult']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function match(value) {
|
|
|
|
|
return new jasmine.Matchers(env, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function detailsFor(actual, matcherName, matcherArgs) {
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
matcher[matcherName].apply(matcher, matcherArgs);
|
|
|
|
|
expect(matcher.results().getItems().length).toEqual(1);
|
|
|
|
|
return matcher.results().getItems()[0].details;
|
|
|
|
|
function match(value) {
|
|
|
|
|
return new jasmine.Matchers(env, value, mockSpec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it("toEqual with primitives, objects, dates, html nodes, etc.", function() {
|
|
|
|
|
@@ -22,8 +16,12 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
|
|
|
|
|
expect(match({foo:'bar'}).toEqual(null)).toEqual(false);
|
|
|
|
|
|
|
|
|
|
var functionA = function() { return 'hi'; };
|
|
|
|
|
var functionB = function() { return 'hi'; };
|
|
|
|
|
var functionA = function() {
|
|
|
|
|
return 'hi';
|
|
|
|
|
};
|
|
|
|
|
var functionB = function() {
|
|
|
|
|
return 'hi';
|
|
|
|
|
};
|
|
|
|
|
expect(match({foo:functionA}).toEqual({foo:functionB})).toEqual(false);
|
|
|
|
|
expect(match({foo:functionA}).toEqual({foo:functionA})).toEqual(true);
|
|
|
|
|
|
|
|
|
|
@@ -49,6 +47,34 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect((match(['a', 'b']).toEqual(['a', 'b', undefined]))).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toEqual to build an Expectation Result", function() {
|
|
|
|
|
var matcher = match('a');
|
|
|
|
|
matcher.toEqual('b');
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toEqual");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch("a");
|
|
|
|
|
expect(result.message).toMatch("b");
|
|
|
|
|
expect(result.expected).toEqual(["b"]);
|
|
|
|
|
expect(result.actual).toEqual("a");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toNotEqual to build an Expectation Result", function() {
|
|
|
|
|
var matcher = match('a');
|
|
|
|
|
matcher.toNotEqual('a');
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotEqual");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(/a/);
|
|
|
|
|
expect(result.message).toMatch(/not/);
|
|
|
|
|
expect(result.expected).toEqual(["a"]);
|
|
|
|
|
expect(result.actual).toEqual("a");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('toBe should return true only if the expected and actual items === each other', function() {
|
|
|
|
|
var a = {};
|
|
|
|
|
var b = {};
|
|
|
|
|
@@ -62,6 +88,35 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect((match(a).toNotBe(c))).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBe to build an ExpectationResult", function() {
|
|
|
|
|
var expected = 'b';
|
|
|
|
|
var actual = 'a';
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
matcher.toBe(expected);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBe");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
|
expect(result.expected).toEqual([expected]);
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toNotBe to build an ExpectationResult", function() {
|
|
|
|
|
var str = 'a';
|
|
|
|
|
var matcher = match(str);
|
|
|
|
|
matcher.toNotBe(str);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotBe");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(str);
|
|
|
|
|
expect(result.expected).toEqual([str]);
|
|
|
|
|
expect(result.actual).toEqual(str);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toMatch and #toNotMatch should perform regular expression matching on strings", function() {
|
|
|
|
|
expect((match('foobarbel').toMatch(/bar/))).toEqual(true);
|
|
|
|
|
@@ -77,17 +132,116 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect((match('foobazbel').toNotMatch("bar"))).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toMatch w/ RegExp to build an ExpectationResult", function() {
|
|
|
|
|
var matcher = match('a');
|
|
|
|
|
matcher.toMatch(/b/);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toMatch");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toEqual("a does not match the regular expression /b/");
|
|
|
|
|
expect(result.expected.toString()).toEqual("/b/");
|
|
|
|
|
expect(result.actual).toEqual("a");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toMatch w/ String to build an ExpectationResult", function() {
|
|
|
|
|
var matcher = match('a');
|
|
|
|
|
matcher.toMatch("b");
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toMatch");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toEqual("a does not match the regular expression /b/");
|
|
|
|
|
expect(result.expected.toString()).toEqual("b");
|
|
|
|
|
expect(result.actual).toEqual("a");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toNotMatch w/ RegExp to build an ExpectationResult", function() {
|
|
|
|
|
var matcher = match('a');
|
|
|
|
|
matcher.toNotMatch(/a/);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotMatch");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch("/a/");
|
|
|
|
|
expect(result.message).toMatch("not");
|
|
|
|
|
expect(result.expected.toString()).toEqual("/a/");
|
|
|
|
|
expect(result.actual).toEqual("a");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toNotMatch w/ String to build an ExpectationResult", function() {
|
|
|
|
|
var matcher = match('a');
|
|
|
|
|
matcher.toNotMatch('a');
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotMatch");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch("/a/");
|
|
|
|
|
expect(result.message).toMatch("not");
|
|
|
|
|
expect(result.expected.toString()).toEqual('a');
|
|
|
|
|
expect(result.actual).toEqual("a");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeDefined", function() {
|
|
|
|
|
expect(match('foo').toBeDefined()).toEqual(true);
|
|
|
|
|
expect(match(undefined).toBeDefined()).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeDefined to build an ExpectationResult", function() {
|
|
|
|
|
var matcher = match(undefined);
|
|
|
|
|
matcher.toBeDefined();
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeDefined");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toEqual('Expected actual to not be undefined.');
|
|
|
|
|
expect(result.actual).toEqual(undefined);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeUndefined", function() {
|
|
|
|
|
expect(match('foo').toBeUndefined()).toEqual(false);
|
|
|
|
|
expect(match(undefined).toBeUndefined()).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeNull", function() {
|
|
|
|
|
expect(match(null).toBeNull()).toEqual(true);
|
|
|
|
|
expect(match(undefined).toBeNull()).toEqual(false);
|
|
|
|
|
expect(match("foo").toBeNull()).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeNull w/ String to build an ExpectationResult", function() {
|
|
|
|
|
var actual = 'a';
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
matcher.toBeNull();
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeNull");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
|
expect(result.message).toMatch('null');
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeNull w/ Object to build an ExpectationResult", function() {
|
|
|
|
|
var actual = {a: 'b'};
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
matcher.toBeNull();
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeNull");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
|
expect(result.message).toMatch('null');
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeFalsy", function() {
|
|
|
|
|
expect(match(false).toBeFalsy()).toEqual(true);
|
|
|
|
|
expect(match(true).toBeFalsy()).toEqual(false);
|
|
|
|
|
@@ -96,6 +250,20 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect(match("").toBeFalsy()).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeFalsy to build an ExpectationResult", function() {
|
|
|
|
|
var actual = 'a';
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
matcher.toBeFalsy();
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeFalsy");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
|
expect(result.message).toMatch('falsy');
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeTruthy", function() {
|
|
|
|
|
expect(match(false).toBeTruthy()).toEqual(false);
|
|
|
|
|
expect(match(true).toBeTruthy()).toEqual(true);
|
|
|
|
|
@@ -107,6 +275,18 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect(match({foo: 1}).toBeTruthy()).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeTruthy to build an ExpectationResult", function() {
|
|
|
|
|
var matcher = match(false);
|
|
|
|
|
matcher.toBeTruthy();
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeTruthy");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toEqual("Expected actual to be truthy");
|
|
|
|
|
expect(result.actual).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toEqual", function() {
|
|
|
|
|
expect(match(undefined).toEqual(undefined)).toEqual(true);
|
|
|
|
|
expect(match({foo:'bar'}).toEqual({foo:'bar'})).toEqual(true);
|
|
|
|
|
@@ -122,10 +302,13 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect(match("foo").toEqual(jasmine.any(Object))).toEqual(false);
|
|
|
|
|
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toEqual(true);
|
|
|
|
|
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toEqual(false);
|
|
|
|
|
expect(match(function() {}).toEqual(jasmine.any(Object))).toEqual(false);
|
|
|
|
|
expect(match(function() {
|
|
|
|
|
}).toEqual(jasmine.any(Object))).toEqual(false);
|
|
|
|
|
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toEqual(true);
|
|
|
|
|
expect(match(function() {}).toEqual(jasmine.any(Function))).toEqual(true);
|
|
|
|
|
expect(match(["a", function() {}]).toEqual(["a", jasmine.any(Function)])).toEqual(true);
|
|
|
|
|
expect(match(function() {
|
|
|
|
|
}).toEqual(jasmine.any(Function))).toEqual(true);
|
|
|
|
|
expect(match(["a", function() {
|
|
|
|
|
}]).toEqual(["a", jasmine.any(Function)])).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toEqual handles circular objects ok", function() {
|
|
|
|
|
@@ -154,7 +337,7 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
it("toContain and toNotContain", function() {
|
|
|
|
|
expect(match('ABC').toContain('A')).toEqual(true);
|
|
|
|
|
expect(match('ABC').toContain('X')).toEqual(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(match(['A', 'B', 'C']).toContain('A')).toEqual(true);
|
|
|
|
|
expect(match(['A', 'B', 'C']).toContain('F')).toEqual(false);
|
|
|
|
|
expect(match(['A', 'B', 'C']).toNotContain('F')).toEqual(true);
|
|
|
|
|
@@ -162,10 +345,40 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
|
|
|
|
|
expect(match(['A', {some:'object'}, 'C']).toContain({some:'object'})).toEqual(true);
|
|
|
|
|
expect(match(['A', {some:'object'}, 'C']).toContain({some:'other object'})).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(detailsFor('abc', 'toContain', ['x'])).toEqual({
|
|
|
|
|
matcherName: 'toContain', expected: 'x', actual: 'abc'
|
|
|
|
|
});
|
|
|
|
|
it("toContain to build an ExpectationResult", function() {
|
|
|
|
|
var actual = ['a','b','c'];
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
var expected = 'x';
|
|
|
|
|
matcher.toContain(expected);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toContain");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
|
expect(result.message).toMatch('contain');
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
expect(result.expected).toEqual([expected]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toNotContain to build an ExpectationResult", function() {
|
|
|
|
|
var actual = ['a','b','c'];
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
var expected = 'b';
|
|
|
|
|
matcher.toNotContain(expected);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotContain");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
|
expect(result.message).toMatch('not contain');
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
expect(result.expected).toEqual([expected]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeLessThan should pass if actual is less than expected", function() {
|
|
|
|
|
@@ -174,141 +387,178 @@ describe("jasmine.Matchers", function() {
|
|
|
|
|
expect(match(37).toBeLessThan(37)).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeLessThan to build an ExpectationResult", function() {
|
|
|
|
|
var actual = 3;
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
var expected = 1;
|
|
|
|
|
matcher.toBeLessThan(expected);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeLessThan");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual) + ' to be less than');
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
expect(result.expected).toEqual([expected]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeGreaterThan should pass if actual is greater than expected", function() {
|
|
|
|
|
expect(match(37).toBeGreaterThan(42)).toEqual(false);
|
|
|
|
|
expect(match(37).toBeGreaterThan(-42)).toEqual(true);
|
|
|
|
|
expect(match(37).toBeGreaterThan(37)).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toBeGreaterThan to build an ExpectationResult", function() {
|
|
|
|
|
var actual = 1;
|
|
|
|
|
var matcher = match(actual);
|
|
|
|
|
var expected = 3;
|
|
|
|
|
matcher.toBeGreaterThan(expected);
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeGreaterThan");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual) + ' to be greater than');
|
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
|
expect(result.expected).toEqual([expected]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("toThrow", function() {
|
|
|
|
|
var expected = new jasmine.Matchers(env, function() {
|
|
|
|
|
throw new Error("Fake Error");
|
|
|
|
|
});
|
|
|
|
|
}, mockSpec);
|
|
|
|
|
expect(expected.toThrow()).toEqual(true);
|
|
|
|
|
expect(expected.toThrow("Fake Error")).toEqual(true);
|
|
|
|
|
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true);
|
|
|
|
|
|
|
|
|
|
expect(expected.toThrow("Other Error")).toEqual(false);
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
expect(result.message).toMatch("Other Error");
|
|
|
|
|
|
|
|
|
|
expect(expected.toThrow(new Error("Other Error"))).toEqual(false);
|
|
|
|
|
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
expect(result.message).toMatch("Other Error");
|
|
|
|
|
|
|
|
|
|
var exception;
|
|
|
|
|
try {
|
|
|
|
|
(function (){
|
|
|
|
|
new jasmine.Matchers(env, 'not-a-function', mockSpec).toThrow();
|
|
|
|
|
})();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
exception = e;
|
|
|
|
|
};
|
|
|
|
|
expect(exception).toBeDefined();
|
|
|
|
|
expect(exception.message).toEqual('Actual is not a function');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(match(function() {
|
|
|
|
|
}).toThrow()).toEqual(false);
|
|
|
|
|
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
expect(result.message).toEqual('Expected function to throw an exception.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(match(function() {}).toThrow()).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("wasCalled, wasNotCalled, wasCalledWith", function() {
|
|
|
|
|
var currentSuite;
|
|
|
|
|
var spec;
|
|
|
|
|
currentSuite = env.describe('default current suite', function() {
|
|
|
|
|
spec = env.it();
|
|
|
|
|
var spyMatch = function(value, spec) {
|
|
|
|
|
return new jasmine.Matchers(env, value, spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("wasCalled, wasNotCalled, wasCalledWith", function() {
|
|
|
|
|
var TestClass;
|
|
|
|
|
beforeEach(function() {
|
|
|
|
|
TestClass = { someFunction: function() {
|
|
|
|
|
} };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should always show an error if the actual is not a spy', function () {
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled()).toEqual(false);
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
expect(result.message).toEqual("Actual is not a spy.");
|
|
|
|
|
|
|
|
|
|
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false);
|
|
|
|
|
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
expect(result.message).toEqual("Actual is not a spy.");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var TestClass = { someFunction: function() {
|
|
|
|
|
} };
|
|
|
|
|
it("should work for spys", function() {
|
|
|
|
|
TestClass.someFunction = jasmine.createSpy("My spy");
|
|
|
|
|
|
|
|
|
|
var expected;
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled()).toEqual(false);
|
|
|
|
|
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false);
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled()).toEqual(false);
|
|
|
|
|
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(true);
|
|
|
|
|
|
|
|
|
|
spec.spyOn(TestClass, 'someFunction');
|
|
|
|
|
TestClass.someFunction();
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled()).toEqual(true);
|
|
|
|
|
expect(function () { match(TestClass.someFunction).wasCalled('some arg');}).toThrow('wasCalled does not take arguments, use wasCalledWith');
|
|
|
|
|
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false);
|
|
|
|
|
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled()).toEqual(false);
|
|
|
|
|
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(true);
|
|
|
|
|
TestClass.someFunction('a', 'b', 'c');
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
|
|
|
|
|
|
|
|
|
|
var expected = match(TestClass.someFunction);
|
|
|
|
|
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
|
|
|
|
|
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
|
|
|
|
|
TestClass.someFunction();
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled()).toEqual(true);
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalled('some arg')).toEqual(false);
|
|
|
|
|
expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false);
|
|
|
|
|
TestClass.someFunction.reset();
|
|
|
|
|
TestClass.someFunction('a', 'b', 'c');
|
|
|
|
|
TestClass.someFunction('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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
TestClass.someFunction('a', 'b', 'c');
|
|
|
|
|
expect(match(TestClass.someFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
|
|
|
|
|
|
|
|
|
|
expected = match(TestClass.someFunction);
|
|
|
|
|
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
|
|
|
|
|
expect(expected.results().getItems()[0].passed()).toEqual(false);
|
|
|
|
|
|
|
|
|
|
TestClass.someFunction.reset();
|
|
|
|
|
TestClass.someFunction('a', 'b', 'c');
|
|
|
|
|
TestClass.someFunction('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(detailsFor(TestClass.someFunction, 'wasCalledWith', ['x', 'y', 'z'])).toEqual({
|
|
|
|
|
matcherName: 'wasCalledWith', expected: ['x', 'y', 'z'], actual: TestClass.someFunction.argsForCall
|
|
|
|
|
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 handle case of actual not being a spy", function() {
|
|
|
|
|
var matcher = match();
|
|
|
|
|
matcher.wasCalledWith('a', 'b');
|
|
|
|
|
|
|
|
|
|
var result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("wasCalledWith");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toEqual("Actual is not a spy");
|
|
|
|
|
expect(result.actual).toEqual(undefined);
|
|
|
|
|
expect(result.expected).toEqual(['a','b']);
|
|
|
|
|
|
|
|
|
|
matcher = match('foo');
|
|
|
|
|
matcher.wasCalledWith('a', 'b');
|
|
|
|
|
|
|
|
|
|
result = mockSpec.addMatcherResult.mostRecentCall.args[0];
|
|
|
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("wasCalledWith");
|
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
|
expect(result.message).toEqual("Actual is not a spy");
|
|
|
|
|
expect(result.actual).toEqual('foo');
|
|
|
|
|
expect(result.expected).toEqual(['a','b']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should report mismatches in some nice way", function() {
|
|
|
|
|
var results = new jasmine.NestedResults();
|
|
|
|
|
var expected = new jasmine.Matchers(env, true, results);
|
|
|
|
|
expected.toEqual(true);
|
|
|
|
|
expected.toEqual(false);
|
|
|
|
|
|
|
|
|
|
expect(results.getItems().length).toEqual(2);
|
|
|
|
|
|
|
|
|
|
expect(results.getItems()[0].passed()).toEqual(true);
|
|
|
|
|
|
|
|
|
|
expect(results.getItems()[1].passed()).toEqual(false);
|
|
|
|
|
|
|
|
|
|
results = new jasmine.NestedResults();
|
|
|
|
|
expected = new jasmine.Matchers(env, false, results);
|
|
|
|
|
expected.toEqual(true);
|
|
|
|
|
|
|
|
|
|
var expectedMessage = 'Expected<br /><br />true<br /><br />but got<br /><br />false<br />';
|
|
|
|
|
expect(results.getItems()[0].message).toEqual(expectedMessage);
|
|
|
|
|
|
|
|
|
|
results = new jasmine.NestedResults();
|
|
|
|
|
expected = new jasmine.Matchers(env, null, results);
|
|
|
|
|
expected.toEqual('not null');
|
|
|
|
|
|
|
|
|
|
expectedMessage = 'Expected<br /><br />\'not null\'<br /><br />but got<br /><br />null<br />';
|
|
|
|
|
expect(results.getItems()[0].message).toEqual(expectedMessage);
|
|
|
|
|
|
|
|
|
|
results = new jasmine.NestedResults();
|
|
|
|
|
expected = new jasmine.Matchers(env, undefined, results);
|
|
|
|
|
expected.toEqual('not undefined');
|
|
|
|
|
|
|
|
|
|
expectedMessage = 'Expected<br /><br />\'not undefined\'<br /><br />but got<br /><br />undefined<br />';
|
|
|
|
|
expect(results.getItems()[0].message).toEqual(expectedMessage);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = new jasmine.NestedResults();
|
|
|
|
|
expected = new jasmine.Matchers(env, {foo:'one',baz:'two', more: 'blah'}, results);
|
|
|
|
|
expected.toEqual({foo:'one', bar: '<b>three</b> &', baz: '2'});
|
|
|
|
|
|
|
|
|
|
expectedMessage =
|
|
|
|
|
"Expected<br /><br />{ foo : 'one', bar : '<b>three</b> &', baz : '2' }<br /><br />but got<br /><br />{ foo : 'one', baz : 'two', more : 'blah' }<br />" +
|
|
|
|
|
"<br /><br />Different Keys:<br />" +
|
|
|
|
|
"expected has key 'bar', but missing from <b>actual</b>.<br />" +
|
|
|
|
|
"<b>expected</b> missing key 'more', but present in actual.<br />" +
|
|
|
|
|
"<br /><br />Different Values:<br />" +
|
|
|
|
|
"'bar' was<br /><br />'<b>three</b> &'<br /><br />in expected, but was<br /><br />'undefined'<br /><br />in actual.<br /><br />" +
|
|
|
|
|
"'baz' was<br /><br />'2'<br /><br />in expected, but was<br /><br />'two'<br /><br />in actual.<br /><br />";
|
|
|
|
|
var actualMessage = results.getItems()[0].message;
|
|
|
|
|
expect(actualMessage).toEqual(expectedMessage);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = new jasmine.NestedResults();
|
|
|
|
|
expected = new jasmine.Matchers(env, true, results);
|
|
|
|
|
expected.toEqual(true);
|
|
|
|
|
|
|
|
|
|
expect(results.getItems()[0].message).toEqual('Passed.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expected = new jasmine.Matchers(env, [1, 2, 3], results);
|
|
|
|
|
results.getItems().length = 0;
|
|
|
|
|
expected.toEqual([1, 2, 3]);
|
|
|
|
|
expect(results.getItems()[0].passed()).toEqual(true);
|
|
|
|
|
|
|
|
|
|
expected = new jasmine.Matchers(env, [1, 2, 3], results);
|
|
|
|
|
results.getItems().length = 0;
|
|
|
|
|
expected.toEqual([{}, {}, {}]);
|
|
|
|
|
expect(results.getItems()[0].passed()).toEqual(false);
|
|
|
|
|
|
|
|
|
|
expected = new jasmine.Matchers(env, [{}, {}, {}], results);
|
|
|
|
|
results.getItems().length = 0;
|
|
|
|
|
expected.toEqual([1, 2, 3]);
|
|
|
|
|
expect(results.getItems()[0].passed()).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|