简体   繁体   中英

Jasmine: Testing variable state before function is called

I am invoking a function that depends on the global window object having several variables defined before it can run successfully by injecting a 3rd party script. ie.

window.foo = 1;
function calledAfter() {
    // inject 3rd party script that depends on
    // window.foo !== undefined
}

Using Jasmine and Sinon.js, how would I test to ensure window.foo is set before calledAfter is invoked?

You wouldn't. This is exactly one of the reasons why storing state globally is a VERY bad idea. If the function depends on a specific configuration-set it should be injected with the set or - less preferably - have a way to request that set. And in general, dependency management isn't unit tested, that's what integration testing is for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM