Deprecate getResults() (use results()). Add some unit test coverage for jasmine.Spec. Add some unit test coverage for JsApiReporterSpec.

This commit is contained in:
ragaskar
2009-09-28 11:13:44 -07:00
parent d09cacebc7
commit 2588368231
39 changed files with 2100 additions and 1518 deletions

View File

@@ -1,4 +1,22 @@
describe("jasmine.Env", function() {
describe('ids', function () {
var env;
beforeEach(function() {
env = new jasmine.Env();
});
it('nextSpecId should return consecutive integers, starting at 0', function () {
expect(env.nextSpecId()).toEqual(0);
expect(env.nextSpecId()).toEqual(1);
expect(env.nextSpecId()).toEqual(2);
});
it('nextSuiteId should return consecutive integers, starting at 0', function () {
expect(env.nextSuiteId()).toEqual(0);
expect(env.nextSuiteId()).toEqual(1);
expect(env.nextSuiteId()).toEqual(2);
});
});
describe("reporting", function() {
var env;
var fakeReporter;

View File

@@ -76,7 +76,7 @@ describe('Exceptions:', function() {
suite.execute();
fakeTimer.tick(2500);
var suiteResults = suite.getResults();
var suiteResults = suite.results();
var specResults = suiteResults.getItems();
expect(suiteResults.passed()).toEqual(false);

View File

@@ -0,0 +1,47 @@
describe('JsApiReporterSpec', function () {
describe('results', function () {
var reporter, spec1, spec2, expectedSpec1Results, expectedSpec2Results;
beforeEach(function() {
var env = new jasmine.Env();
var suite = new jasmine.Suite(env);
spec1 = new jasmine.Spec(env, suite);
spec1.runs(function () {
this.expect(true).toEqual(true);
});
expectedSpec1Results = {
messages: spec1.results().getItems(),
result: "passed"
};
spec2 = new jasmine.Spec(env, suite);
spec2.runs(function () {
this.expect(true).toEqual(false);
});
expectedSpec2Results = {
messages: spec2.results().getItems(),
result: "failed"
};
spec1.execute();
spec2.execute();
reporter = new jasmine.JsApiReporter();
reporter.reportSpecResults(spec1);
reporter.reportSpecResults(spec2);
});
it('resultForSpec() should return the result for the given spec', function () {
expect(reporter.resultsForSpec(spec1.id)).toEqual(expectedSpec1Results);
expect(reporter.resultsForSpec(spec2.id)).toEqual(expectedSpec2Results);
});
it('results() should return a hash of all results, indexed by spec id', function () {
expect(reporter.results()[spec1.id]).toEqual(expectedSpec1Results);
expect(reporter.results()[spec2.id]).toEqual(expectedSpec2Results);
});
});
});

View File

@@ -12,8 +12,8 @@ describe("jasmine.Matchers", function() {
function detailsFor(actual, matcherName, matcherArgs) {
var matcher = match(actual);
matcher[matcherName].apply(matcher, matcherArgs);
expect(matcher.getResults().getItems().length).toEqual(1);
return matcher.getResults().getItems()[0].details;
expect(matcher.results().getItems().length).toEqual(1);
return matcher.results().getItems()[0].details;
}
it("toEqual with primitives, objects, dates, html nodes, etc.", function() {
@@ -222,7 +222,7 @@ describe("jasmine.Matchers", function() {
expected = match(TestClass.someFunction);
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
expect(expected.getResults().getItems()[0].passed()).toEqual(false);
expect(expected.results().getItems()[0].passed()).toEqual(false);
TestClass.someFunction.reset();
TestClass.someFunction('a', 'b', 'c');

View File

@@ -31,7 +31,7 @@ describe('RunnerTest', function() {
env.currentRunner.execute();
var runnerResults = env.currentRunner.getResults();
var runnerResults = env.currentRunner.results();
expect(runnerResults.totalCount).toEqual(2);
expect(runnerResults.passedCount).toEqual(1);
expect(runnerResults.failedCount).toEqual(1);
@@ -57,7 +57,7 @@ describe('RunnerTest', function() {
env.currentRunner.execute();
var runnerResults = env.currentRunner.getResults();
var runnerResults = env.currentRunner.results();
expect(runnerResults.totalCount).toEqual(1);
expect(runnerResults.passedCount).toEqual(0);
expect(runnerResults.failedCount).toEqual(1);
@@ -82,7 +82,7 @@ describe('RunnerTest', function() {
env.currentRunner.execute();
var results = env.currentRunner.getResults();
var results = env.currentRunner.results();
expect(results.totalCount).toEqual(2);
expect(results.passedCount).toEqual(1);
expect(results.failedCount).toEqual(1);
@@ -119,7 +119,7 @@ describe('RunnerTest', function() {
//This blows up the JSApiReporter.
//expect(fakeReporter.reportRunnerResults).wasCalledWith(env.currentRunner);
expect(fakeReporter.reportRunnerResults).wasCalled();
expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].getResults()).toEqual(env.currentRunner.getResults());
expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].results()).toEqual(env.currentRunner.results());
});

View File

@@ -67,13 +67,13 @@ describe("jasmine spec running", function () {
expect(specWithNoBody.description).toEqual('new spec');
expect(specWithExpectation.results.getItems().length).toEqual(1); // "Results aren't there after a spec was executed"
expect(specWithExpectation.results.getItems()[0].passed()).toEqual(true); // "Results has a result, but it's true"
expect(specWithExpectation.results.description).toEqual('spec with an expectation'); // "Spec's results did not get the spec's description"
expect(specWithExpectation.results().getItems().length).toEqual(1); // "Results aren't there after a spec was executed"
expect(specWithExpectation.results().getItems()[0].passed()).toEqual(true); // "Results has a result, but it's true"
expect(specWithExpectation.results().description).toEqual('spec with an expectation'); // "Spec's results did not get the spec's description"
expect(specWithFailingExpectations.results.getItems()[0].passed()).toEqual(false); // "Expectation that failed, passed"
expect(specWithFailingExpectations.results().getItems()[0].passed()).toEqual(false); // "Expectation that failed, passed"
expect(specWithMultipleExpectations.results.getItems().length).toEqual(2); // "Spec doesn't support multiple expectations"
expect(specWithMultipleExpectations.results().getItems().length).toEqual(2); // "Spec doesn't support multiple expectations"
});
it("should work without a runs block", function() {
@@ -89,10 +89,10 @@ describe("jasmine spec running", function () {
another_spec.execute();
another_spec.done = true;
expect(another_spec.results.getItems().length).toEqual(2);
expect(another_spec.results.getItems()[0].passed()).toEqual(true); // "In a spec without a run block, expected first expectation result to be true but was false"
expect(another_spec.results.getItems()[1].passed()).toEqual(false); // "In a spec without a run block, expected second expectation result to be false but was true";
expect(another_spec.results.description).toEqual('spec with an expectation'); // "In a spec without a run block, results did not include the spec's description";
expect(another_spec.results().getItems().length).toEqual(2);
expect(another_spec.results().getItems()[0].passed()).toEqual(true); // "In a spec without a run block, expected first expectation result to be true but was false"
expect(another_spec.results().getItems()[1].passed()).toEqual(false); // "In a spec without a run block, expected second expectation result to be false but was true";
expect(another_spec.results().description).toEqual('spec with an expectation'); // "In a spec without a run block, results did not include the spec's description";
});
it('should queue waits and runs that it encounters while executing specs', function() {
@@ -141,8 +141,8 @@ describe("jasmine spec running", function () {
a_spec.execute();
expect(a_spec.results.getItems().length).toEqual(1); // 'No call to waits(): Spec queue did not run all functions';
expect(a_spec.results.getItems()[0].passed()).toEqual(true); // 'No call to waits(): Queued expectation failed';
expect(a_spec.results().getItems().length).toEqual(1); // 'No call to waits(): Spec queue did not run all functions';
expect(a_spec.results().getItems()[0].passed()).toEqual(true); // 'No call to waits(): Queued expectation failed';
foo = 0;
env.describe('test async spec', function() {
@@ -161,15 +161,15 @@ describe("jasmine spec running", function () {
a_spec.execute();
expect(a_spec.results.getItems().length).toEqual(0);
expect(a_spec.results().getItems().length).toEqual(0);
fakeTimer.tick(500);
expect(a_spec.results.getItems().length).toEqual(0);
expect(a_spec.results().getItems().length).toEqual(0);
fakeTimer.tick(500);
expect(a_spec.results.getItems().length).toEqual(1); // 'Calling waits(): Spec queue did not run all functions';
expect(a_spec.results().getItems().length).toEqual(1); // 'Calling waits(): Spec queue did not run all functions';
expect(a_spec.results.getItems()[0].passed()).toEqual(true); // 'Calling waits(): Queued expectation failed';
expect(a_spec.results().getItems()[0].passed()).toEqual(true); // 'Calling waits(): Queued expectation failed';
var bar = 0;
var another_spec;
@@ -199,8 +199,8 @@ describe("jasmine spec running", function () {
fakeTimer.tick(1000);
expect(another_spec.results.getItems().length).toEqual(1);
expect(another_spec.results.getItems()[0].passed()).toEqual(true);
expect(another_spec.results().getItems().length).toEqual(1);
expect(another_spec.results().getItems()[0].passed()).toEqual(true);
var baz = 0;
var yet_another_spec;
@@ -225,8 +225,8 @@ describe("jasmine spec running", function () {
fakeTimer.tick(150);
expect(yet_another_spec.results.getItems().length).toEqual(1);
expect(yet_another_spec.results.getItems()[0].passed()).toEqual(false);
expect(yet_another_spec.results().getItems().length).toEqual(1);
expect(yet_another_spec.results().getItems()[0].passed()).toEqual(false);
});
it("testAsyncSpecsWithMockSuite", function () {
@@ -254,8 +254,8 @@ describe("jasmine spec running", function () {
another_spec.execute();
fakeTimer.tick(2000);
expect(another_spec.results.getItems().length).toEqual(1);
expect(another_spec.results.getItems()[0].passed()).toEqual(true);
expect(another_spec.results().getItems().length).toEqual(1);
expect(another_spec.results().getItems()[0].passed()).toEqual(true);
});
it("testWaitsFor", function() {
@@ -304,7 +304,7 @@ describe("jasmine spec running", function () {
spec.execute();
fakeTimer.tick(1000);
var actual = spec.results.getItems()[0].message;
var actual = spec.results().getItems()[0].message;
var expected = 'timeout: timed out after 500 msec waiting for my awesome condition';
expect(actual).toEqual(expected);
});
@@ -334,7 +334,7 @@ describe("jasmine spec running", function () {
expect(runsBlockExecuted).toEqual(false);
fakeTimer.tick(400);
expect(runsBlockExecuted).toEqual(false);
var actual = spec.results.getItems()[0].message;
var actual = spec.results().getItems()[0].message;
var expected = 'timeout: timed out after 500 msec waiting for something to happen';
expect(actual).toEqual(expected,
'expected "' + expected + '" but found "' + actual + '"');
@@ -425,8 +425,8 @@ describe("jasmine spec running", function () {
var suite = suiteWithBefore;
expect(suite.getResults().getItems()[0].passed()).toEqual(true); // "testBeforeAndAfterCallbacks: the first spec's foo should have been 2");
expect(suite.getResults().getItems()[1].passed()).toEqual(true); // "testBeforeAndAfterCallbacks: the second spec's this.foo should have been 2");
expect(suite.results().getItems()[0].passed()).toEqual(true); // "testBeforeAndAfterCallbacks: the first spec's foo should have been 2");
expect(suite.results().getItems()[1].passed()).toEqual(true); // "testBeforeAndAfterCallbacks: the second spec's this.foo should have been 2");
var foo = 1;
@@ -453,8 +453,8 @@ describe("jasmine spec running", function () {
suite = suiteWithAfter;
expect(suite.afterEach.length).toEqual(1);
expect(suite.getResults().getItems()[0].passed()).toEqual(true);
expect(suite.getResults().getItems()[1].passed()).toEqual(true);
expect(suite.results().getItems()[0].passed()).toEqual(true);
expect(suite.results().getItems()[1].passed()).toEqual(true);
expect(foo).toEqual(0);
});
@@ -809,7 +809,7 @@ describe("jasmine spec running", function () {
suite.execute();
expect(report).toEqual("firstsecond");
var suiteResults = suite.getResults();
var suiteResults = suite.results();
expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(false);
expect(suiteResults.getItems()[1].getItems()[0].passed()).toEqual(true);
});
@@ -848,7 +848,7 @@ describe("jasmine spec running", function () {
expect(report).toEqual("firstsecondthird"); // "all tests should run");
//After each errors should not go in spec results because it confuses the count.
var suiteResults = suite.getResults();
var suiteResults = suite.results();
expect(suiteResults.getItems().length).toEqual(3, 'testAfterExecutesSafely should have results for three specs');
expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 1st spec should pass");
@@ -973,7 +973,7 @@ describe("jasmine spec running", function () {
env.execute();
var runnerResults = env.currentRunner.getResults();
var runnerResults = env.currentRunner.results();
expect(runnerResults.totalCount).toEqual(3);
expect(runnerResults.passedCount).toEqual(3);
expect(runnerResults.failedCount).toEqual(0);
@@ -1011,7 +1011,7 @@ describe("jasmine spec running", function () {
suite.execute();
fakeTimer.tick(600);
expect(spec.foo).toEqual(2);
var suiteResults = suite.getResults();
var suiteResults = suite.results();
expect(suiteResults.getItems()[0].getItems().length).toEqual(2);
expect(suiteResults.getItems()[0].getItems()[0].passed()).toEqual(false);
expect(suiteResults.getItems()[0].getItems()[1].passed()).toEqual(true);

109
spec/suites/SpecSpec.js Normal file
View File

@@ -0,0 +1,109 @@
describe('Spec', function () {
var env, suite;
beforeEach(function() {
env = new jasmine.Env();
suite = new jasmine.Suite(env, 'suite 1');
});
describe('initialization', function () {
it('should raise an error if an env is not passed', function () {
try {
new jasmine.Spec();
}
catch (e) {
expect(e.message).toEqual('jasmine.Env() required');
}
});
it('should raise an error if a suite is not passed', function () {
try {
new jasmine.Spec(env);
}
catch (e) {
expect(e.message).toEqual('jasmine.Suite() required');
}
});
it('should assign sequential ids for specs belonging to the same env', function () {
var spec1 = new jasmine.Spec(env, suite);
var spec2 = new jasmine.Spec(env, suite);
var spec3 = new jasmine.Spec(env, suite);
expect(spec1.id).toEqual(0);
expect(spec2.id).toEqual(1);
expect(spec3.id).toEqual(2);
});
});
it('getFullName returns suite & spec description', function () {
var spec = new jasmine.Spec(env, suite, 'spec 1');
expect(spec.getFullName()).toEqual('suite 1 spec 1.')
});
describe('results', function () {
var spec, results;
beforeEach(function () {
spec = new jasmine.Spec(env, suite);
results = spec.results();
expect(results.totalCount).toEqual(0);
spec.runs(function () {
this.expect(true).toEqual(true);
this.expect(true).toEqual(true);
});
});
it('results shows the total number of expectations for each spec after execution', function () {
expect(results.totalCount).toEqual(0);
spec.execute();
expect(results.totalCount).toEqual(2);
});
it('results shows the number of passed expectations for each spec after execution', function () {
expect(results.passedCount).toEqual(0);
spec.execute();
expect(results.passedCount).toEqual(2);
});
it('results shows the number of failed expectations for each spec after execution', function () {
spec.runs(function () {
this.expect(true).toEqual(false);
});
expect(results.failedCount).toEqual(0);
spec.execute();
expect(results.failedCount).toEqual(1);
});
describe('results.passed', function () {
it('is true if all spec expectations pass', function () {
spec.runs(function () {
this.expect(true).toEqual(true);
});
spec.execute();
expect(results.passed()).toEqual(true);
});
it('is false if one spec expectation fails', function () {
spec.runs(function () {
this.expect(true).toEqual(false);
});
spec.execute();
expect(results.passed()).toEqual(false);
});
it('a spec with no expectations will return true', function () {
var specWithoutExpectations = new jasmine.Spec(env, suite);
specWithoutExpectations.runs(function() {
});
specWithoutExpectations.execute();
expect(results.passed()).toEqual(true);
});
it('an unexecuted spec will return true', function () {
expect(results.passed()).toEqual(true);
});
});
});
});

View File

@@ -31,4 +31,5 @@ describe("TrivialReporter", function() {
expect(divs.length).toEqual(2);
expect(divs[1].innerHTML).toContain("suite 1");
});
});