質問

Suppose I have a function

function f_createScriptElement(f_url) {
        var script = d[CreateElement](Script);
        script.type = "text/javascript";
        script[Src] = f_url;
        script.async = true;

        try {
            var container = ((m_videoPlayerTagId) ? d[GetElementById](m_videoPlayerTagId) : f_getAdContainerElem());
            if (container) {
                container.addEventListener("loadedmetadata", function(e) {
                    var c_width = this.videoWidth,
                        c_height = this.videoHeight,
                        v_width = this[WIDTH],
                        v_height = this[HEIGHT];

                    m_playerProperties[NS_AD_CD] = container.duration;
                    m_playerProperties[NS_AD_SZ] = c_width + "x" + c_height;
                    m_playerProperties[NS_AD_PS] = v_width + "x" + v_height;
                    m_playerProperties[NS_AD_VS] = ((container.volume === 0) ? false : true);

                }, false);

                container.parentNode.insertBefore(script, container.nextSibling);
            } else {
                return;
            }
        } catch (err) {}
    }

It would be great if you could tell the appropriate test cases you would write(Both Positive and Negative) to have complete code coverage for this function in QUnit since it doesnt return a string or a number.

役に立ちましたか?

解決

As this is a function with a side effect you have to test the side effect, which is the inserting of the script element into the DOM. To test the positive case, add a DOM element, which will make the if(container) statement pass and check that the script tag with right attributes was added to the DOM. For the negative just run the test and check that the script element is missing in the DOM.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top