I think I would take an approach to dependency injection. Instead:
function myFunction() { var userAgent = navigator.userAgent;
Maybe 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” getUserAgent()
by doing:
function getUserAgentReal() { // formerly not 'Real' // ... } function getUserAgent() { // formerly 'Mock' // ... }
This design is still not fully automated (you need to manually rename the getter to do your testing), and it adds a ton of complexity to something as simple as working on navigator.userAgent
, and I'm not sure how you would actually identify errors in myFunction
, but I just thought that I would throw it there to give you some ideas how this can be solved.
Maybe the idea of “dependency injection” presented here can somehow be integrated with FireUnit.
Grant Wagner Aug 20 '09 at 16:43 2009-08-20 16:43
source share