I guess I'd take a dependency injection approach. Instead of:
function myFunction() {
var userAgent = navigator.userAgent;
// do stuff with userAgent
}
Maybe do something like:
function myFunction(userAgent) {
// do stuff with userAgent
}
function getUserAgent() {
window.userAgentReal = +window.userAgentReal || 0;
return [ navigator.userAgent ][window.userAgentReal++];
}
function getUserAgentMock() {
window.nextUserAgentMock = +window.nextUserAgentMock || 0;
return [
'test user agent1',
'test user agent2',
'test user agent3'
][window.nextUserAgentMock++];
}
var userAgent;
while (userAgent = getUserAgent()) {
myFunction(userAgent);
}
Then you can "mock out" getUserAgent()
by doing:
function getUserAgentReal() { // formerly not 'Real'
// ...
}
function getUserAgent() { // formerly 'Mock'
// ...
}
This design still isn't completely automated (you have to manually rename the getter to perform your testing), and it adds a bunch of complexity to something as simple as operating on navigator.userAgent
, and I'm not sure how you'd actually identify any bugs in myFunction
, but I just figured I'd throw it out there to give you some ideas how this might be dealt with.
Maybe the idea of "dependency injection" presented here can somehow be integrated with FireUnit.