Manage spys/matchers/custom equalities for beforeAll

- Refactor expectations to take list of matchers
- Add spyRegistry to manage runnables' spies
- Add clone util

[#66789174]
This commit is contained in:
Christopher Amavisca, Greg Cobb and Sheel Choksi
2014-03-05 10:28:37 -08:00
parent 52026fb0f7
commit 752a36d3ff
12 changed files with 441 additions and 272 deletions

View File

@@ -38,13 +38,13 @@ describe("Custom Matchers (Integration)", function() {
});
it("passes the spec if the custom matcher passes", function(done) {
env.addMatchers({
toBeReal: function() {
return { compare: function() { return { pass: true }; } };
}
});
env.it("spec using custom matcher", function() {
env.addMatchers({
toBeReal: function() {
return { compare: function() { return { pass: true }; } };
}
});
env.expect(true).toBeReal();
});
@@ -57,16 +57,16 @@ describe("Custom Matchers (Integration)", function() {
});
it("uses the negative compare function for a negative comparison, if provided", function(done) {
env.addMatchers({
toBeReal: function() {
return {
compare: function() { return { pass: true }; },
negativeCompare: function() { return { pass: true }; }
};
}
});
env.it("spec with custom negative comparison matcher", function() {
env.addMatchers({
toBeReal: function() {
return {
compare: function() { return { pass: true }; },
negativeCompare: function() { return { pass: true }; }
};
}
});
env.expect(true).not.toBeReal();
});
@@ -79,17 +79,17 @@ describe("Custom Matchers (Integration)", function() {
});
it("generates messages with the same rules as built in matchers absent a custom message", function(done) {
env.addMatchers({
toBeReal: function() {
return {
compare: function() {
return { pass: false };
env.it('spec with an expectation', function() {
env.addMatchers({
toBeReal: function() {
return {
compare: function() {
return { pass: false };
}
}
}
}
});
});
env.it('spec with an expectation', function() {
env.expect("a").toBeReal();
});
@@ -103,13 +103,14 @@ describe("Custom Matchers (Integration)", function() {
it("passes the expected and actual arguments to the comparison function", function(done) {
var argumentSpy = jasmine.createSpy("argument spy").and.returnValue({ pass: true });
env.addMatchers({
toBeReal: function() {
return { compare: argumentSpy };
}
});
env.it('spec with an expectation', function () {
env.addMatchers({
toBeReal: function() {
return { compare: argumentSpy };
}
});
env.expect(true).toBeReal();
env.expect(true).toBeReal("arg");
env.expect(true).toBeReal("arg1", "arg2");
@@ -130,12 +131,13 @@ describe("Custom Matchers (Integration)", function() {
argumentSpy = jasmine.createSpy("argument spy").and.returnValue(matcherFactory),
customEqualityFn = function() { return true; };
env.addCustomEqualityTester(customEqualityFn);
env.addMatchers({
toBeReal: argumentSpy
});
env.it("spec with expectation", function() {
env.addCustomEqualityTester(customEqualityFn);
env.addMatchers({
toBeReal: argumentSpy
});
env.expect(true).toBeReal();
});

View File

@@ -326,30 +326,93 @@ describe("Env integration", function() {
env.execute([secondSuite.id, firstSpec.id]);
});
it("Functions can be spied on and have their calls tracked", function () {
it("Functions can be spied on and have their calls tracked", function (done) {
var env = new j$.Env();
var originalFunctionWasCalled = false;
var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } };
var spy = env.spyOn(subject, 'spiedFunc');
env.addReporter({jasmineDone: done});
expect(subject.spiedFunc).toEqual(spy);
env.it("works with spies", function() {
var spy = env.spyOn(subject, 'spiedFunc');
expect(subject.spiedFunc.calls.any()).toEqual(false);
expect(subject.spiedFunc.calls.count()).toEqual(0);
expect(subject.spiedFunc).toEqual(spy);
expect(subject.spiedFunc.calls.any()).toEqual(false);
expect(subject.spiedFunc.calls.count()).toEqual(0);
subject.spiedFunc('foo');
subject.spiedFunc('foo');
expect(subject.spiedFunc.calls.any()).toEqual(true);
expect(subject.spiedFunc.calls.count()).toEqual(1);
expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['foo']);
expect(subject.spiedFunc.calls.mostRecent().object).toEqual(subject);
expect(originalFunctionWasCalled).toEqual(false);
expect(subject.spiedFunc.calls.any()).toEqual(true);
expect(subject.spiedFunc.calls.count()).toEqual(1);
expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['foo']);
expect(subject.spiedFunc.calls.mostRecent().object).toEqual(subject);
expect(originalFunctionWasCalled).toEqual(false);
subject.spiedFunc('bar');
expect(subject.spiedFunc.calls.count()).toEqual(2);
expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['bar']);
subject.spiedFunc('bar');
expect(subject.spiedFunc.calls.count()).toEqual(2);
expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['bar']);
});
env.execute();
});
it('removes all spies added in a spec after the spec is complete', function(done) {
var env = new j$.Env(),
originalFoo = function() {},
testObj = {
foo: originalFoo
},
firstSpec = jasmine.createSpy('firstSpec').and.callFake(function() {
env.spyOn(testObj, 'foo');
}),
secondSpec = jasmine.createSpy('secondSpec').and.callFake(function() {
expect(testObj.foo).toBe(originalFoo);
});
env.describe('test suite', function() {
env.it('spec 0', firstSpec);
env.it('spec 1', secondSpec);
});
var assertions = function() {
expect(firstSpec).toHaveBeenCalled();
expect(secondSpec).toHaveBeenCalled();
done();
};
env.addReporter({ jasmineDone: assertions });
env.execute();
});
it('removes all spies added in a suite after the suite is complete', function(done) {
var env = new j$.Env(),
originalFoo = function() {},
testObj = {
foo: originalFoo
};
env.describe('test suite', function() {
env.beforeAll(function() { env.spyOn(testObj, 'foo');})
env.it('spec 0', function() {
expect(j$.isSpy(testObj.foo)).toBe(true);
});
env.it('spec 1', function() {
expect(j$.isSpy(testObj.foo)).toBe(true);
});
});
env.describe('another suite', function() {
env.it('spec 2', function() {
expect(j$.isSpy(testObj.foo)).toBe(false);
});
});
env.addReporter({ jasmineDone: done });
env.execute();
});
it("Mock clock can be installed and used in tests", function(done) {
@@ -542,11 +605,7 @@ describe("Env integration", function() {
it("Custom equality testers should be per spec", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
reporter = jasmine.createSpyObj('fakeReporter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
@@ -576,30 +635,42 @@ describe("Env integration", function() {
env.execute();
});
it("Custom matchers should be per spec", function() {
it("Custom equality testers should be per suite", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
matchers = {
toFoo: function() {}
},
reporter = jasmine.createSpyObj('fakeReporter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
reporter.jasmineDone.and.callFake(function() {
var firstSpecResult = reporter.specDone.calls.first().args[0],
secondSpecResult = reporter.specDone.calls.argsFor(0)[0],
thirdSpecResult = reporter.specDone.calls.mostRecent().args[0];
expect(firstSpecResult.status).toEqual("passed");
expect(secondSpecResult.status).toEqual("passed");
expect(thirdSpecResult.status).toEqual("failed");
done();
});
env.addReporter(reporter);
env.describe("testing custom matchers", function() {
env.it("with a custom matcher", function() {
env.addMatchers(matchers);
expect(env.expect().toFoo).toBeDefined();
env.describe("testing custom equality testers", function() {
env.beforeAll(function() { env.addCustomEqualityTester(function(a, b) { return true; }); });
env.it("with a custom tester", function() {
env.expect("a").toEqual("b");
});
env.it("without a custom matcher", function() {
expect(env.expect().toFoo).toBeUndefined();
env.it("with the same custom tester", function() {
env.expect("a").toEqual("b");
});
});
env.describe("another suite", function() {
env.it("without the custom tester", function(){
env.expect("a").toEqual("b");
});
});
@@ -609,11 +680,7 @@ describe("Env integration", function() {
it("Custom equality testers for toContain should be per spec", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
reporter = jasmine.createSpyObj('fakeReporter', [
"jasmineStarted",
"jasmineDone",
"suiteStarted",
"suiteDone",
"specStarted",
"specDone"
]);
@@ -636,11 +703,104 @@ describe("Env integration", function() {
});
env.it("without a custom tester", function() {
env.expect("a").toContain("b");
env.expect(["a"]).toContain("b");
});
});
env.execute();
});
it("Custom equality testers for toContain should be per suite", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
reporter = jasmine.createSpyObj('fakeReporter', [
"jasmineDone",
"specDone"
]);
reporter.jasmineDone.and.callFake(function() {
var firstSpecResult = reporter.specDone.calls.first().args[0],
secondSpecResult = reporter.specDone.calls.argsFor(1)[0],
thirdSpecResult = reporter.specDone.calls.mostRecent().args[0];
expect(firstSpecResult.status).toEqual("passed");
expect(secondSpecResult.status).toEqual("passed");
expect(thirdSpecResult.status).toEqual("failed");
done();
});
env.addReporter(reporter);
env.describe("testing custom equality testers", function() {
env.beforeAll(function() { env.addCustomEqualityTester(function(a, b) { return true; })});
env.it("with a custom tester", function() {
env.expect(["a"]).toContain("b");
});
env.it("also with the custom tester", function() {
env.expect(["a"]).toContain("b");
});
});
env.describe("another suite", function() {
env.it("without the custom tester", function() {
env.expect(["a"]).toContain("b");
});
});
env.execute();
});
it("Custom matchers should be per spec", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
matchers = {
toFoo: function() {}
};
env.describe("testing custom matchers", function() {
env.it("with a custom matcher", function() {
env.addMatchers(matchers);
expect(env.expect().toFoo).toBeDefined();
});
env.it("without a custom matcher", function() {
expect(env.expect().toFoo).toBeUndefined();
});
});
env.addReporter({jasmineDone: done});
env.execute();
});
it("Custom matchers should be per suite", function(done) {
var env = new j$.Env({global: { setTimeout: setTimeout }}),
matchers = {
toFoo: function() {}
};
env.describe("testing custom matchers", function() {
env.beforeAll(function() { env.addMatchers(matchers); });
env.it("with a custom matcher", function() {
expect(env.expect().toFoo).toBeDefined();
});
env.it("with the same custom matcher", function() {
expect(env.expect().toFoo).toBeDefined();
});
});
env.describe("another suite", function() {
env.it("no longer has the custom matcher", function() {
expect(env.expect().toFoo).not.toBeDefined();
});
});
env.addReporter({jasmineDone: done});
env.execute();
});
});

View File

@@ -236,11 +236,14 @@ describe("jasmine spec running", function () {
});
});
suite.execute(function() {
var assertions = function() {
expect(specInADisabledSuite).not.toHaveBeenCalled();
done();
});
});
};
env.addReporter({jasmineDone: assertions});
env.execute();
it("should set all pending specs to pending when a suite is run", function(done) {
var pendingSpec,