Fixed bug where multiple calls to jasmine.Clock.useMock cause multiple tear downs

This commit is contained in:
Lee Byrd & Christian Williams 2010-06-23 11:03:05 -07:00
parent 6a9f74e849
commit 9c8180d8d0
4 changed files with 46 additions and 17 deletions

View File

@ -409,6 +409,11 @@ jasmine.createSpyObj = function(baseName, methodNames) {
return obj;
};
/**
* All parameters are pretty-printed and concatenated together, then written to the current spec's output.
*
* Be careful not to leave calls to <code>jasmine.log</code> in production code.
*/
jasmine.log = function() {
var spec = jasmine.getEnv().currentSpec;
spec.log.apply(spec, arguments);
@ -1817,6 +1822,11 @@ jasmine.Spec.prototype.results = function() {
return this.results_;
};
/**
* All parameters are pretty-printed and concatenated together, then written to the spec's output.
*
* Be careful not to leave calls to <code>jasmine.log</code> in production code.
*/
jasmine.Spec.prototype.log = function() {
return this.results_.log(arguments);
};
@ -2180,9 +2190,9 @@ jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMil
funcToRun.funcToCall();
if (funcToRun.recurring) {
this.scheduleFunction(funcToRun.timeoutKey,
funcToRun.funcToCall,
funcToRun.millis,
true);
funcToRun.funcToCall,
funcToRun.millis,
true);
}
} catch(e) {
}
@ -2226,10 +2236,12 @@ jasmine.Clock = {
},
useMock: function() {
var spec = jasmine.getEnv().currentSpec;
spec.after(jasmine.Clock.uninstallMock);
if (!jasmine.Clock.isInstalled()) {
var spec = jasmine.getEnv().currentSpec;
spec.after(jasmine.Clock.uninstallMock);
jasmine.Clock.installMock();
jasmine.Clock.installMock();
}
},
installMock: function() {
@ -2237,6 +2249,7 @@ jasmine.Clock = {
},
uninstallMock: function() {
jasmine.log("uninstall")
jasmine.Clock.assertInstalled();
jasmine.Clock.installed = jasmine.Clock.real;
},
@ -2249,11 +2262,15 @@ jasmine.Clock = {
},
assertInstalled: function() {
if (jasmine.Clock.installed != jasmine.Clock.defaultFakeTimer) {
if (!jasmine.Clock.isInstalled()) {
throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()");
}
},
isInstalled: function() {
return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer;
},
installed: null
};
jasmine.Clock.installed = jasmine.Clock.real;
@ -2287,7 +2304,7 @@ jasmine.getGlobal().clearInterval = function(timeoutKey) {
if (jasmine.Clock.installed.clearTimeout.apply) {
return jasmine.Clock.installed.clearInterval.apply(this, arguments);
} else {
return jasmine.Clock.installed.clearInterval(timeoutKey);
return jasmine.Clock.installed.clearInterval(timeoutKey);
}
};
@ -2296,5 +2313,5 @@ jasmine.version_= {
"major": 0,
"minor": 11,
"build": 0,
"revision": 1277253997
"revision": 1277316068
};

View File

@ -39,6 +39,7 @@
<script type="text/javascript" src="suites/ExceptionsSpec.js"></script>
<script type="text/javascript" src="suites/JsApiReporterSpec.js"></script>
<script type="text/javascript" src="suites/MatchersSpec.js"></script>
<script type="text/javascript" src="suites/MockClockSpec.js"></script>
<script type="text/javascript" src="suites/MultiReporterSpec.js"></script>
<script type="text/javascript" src="suites/NestedResultsSpec.js"></script>
<script type="text/javascript" src="suites/PrettyPrintSpec.js"></script>

View File

@ -31,4 +31,8 @@ describe("MockClock", function () {
expect(interval).toEqual(2);
});
});
it("shouldn't complain if you call jasmine.Clock.useMock() more than once", function() {
jasmine.Clock.useMock();
});
});

View File

@ -64,9 +64,9 @@ jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMil
funcToRun.funcToCall();
if (funcToRun.recurring) {
this.scheduleFunction(funcToRun.timeoutKey,
funcToRun.funcToCall,
funcToRun.millis,
true);
funcToRun.funcToCall,
funcToRun.millis,
true);
}
} catch(e) {
}
@ -110,10 +110,12 @@ jasmine.Clock = {
},
useMock: function() {
var spec = jasmine.getEnv().currentSpec;
spec.after(jasmine.Clock.uninstallMock);
if (!jasmine.Clock.isInstalled()) {
var spec = jasmine.getEnv().currentSpec;
spec.after(jasmine.Clock.uninstallMock);
jasmine.Clock.installMock();
jasmine.Clock.installMock();
}
},
installMock: function() {
@ -121,6 +123,7 @@ jasmine.Clock = {
},
uninstallMock: function() {
jasmine.log("uninstall")
jasmine.Clock.assertInstalled();
jasmine.Clock.installed = jasmine.Clock.real;
},
@ -133,11 +136,15 @@ jasmine.Clock = {
},
assertInstalled: function() {
if (jasmine.Clock.installed != jasmine.Clock.defaultFakeTimer) {
if (!jasmine.Clock.isInstalled()) {
throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()");
}
},
isInstalled: function() {
return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer;
},
installed: null
};
jasmine.Clock.installed = jasmine.Clock.real;
@ -171,7 +178,7 @@ jasmine.getGlobal().clearInterval = function(timeoutKey) {
if (jasmine.Clock.installed.clearTimeout.apply) {
return jasmine.Clock.installed.clearInterval.apply(this, arguments);
} else {
return jasmine.Clock.installed.clearInterval(timeoutKey);
return jasmine.Clock.installed.clearInterval(timeoutKey);
}
};