yaoguo22 / closure-library

Automatically exported from code.google.com/p/closure-library
0 stars 0 forks source link

sessionStorage in jsunit #496

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello everybody.

I am using jsunit to test js functions.

And in the js functions,the global object "sessionStorage" is used to save and 
get key-values.

I write jsunit test methods in the page with the name of "JSTester.html".

But error occurs in jsunit run result.
Error message is like "The sessionStorage object is null."

Anyone knows about that error ?
Thank you very much.

Original issue reported on code.google.com by sunys...@gmail.com on 12 Sep 2012 at 9:49

GoogleCodeExporter commented 9 years ago
It seems like that the "sessionStorage" can not be recognized in JsUnit.
So, it should be MOCKed in JsUnit test.
The mock method is like :
---------------------------------
    var mockup = function() {
        var table = {};
        return {
            getItem : function(key) {
                return table[key];
            },
            setItem : function(key, value) {
                table[key] = value;
            },
            clear : function() {
                table = {};
            }
        };
    }();
    Object.defineProperty(window, 'sessionStorage', {
        value : mockup
    });
-------------------------------------------
Refrence to this page:
http://stackoverflow.com/questions/9428210/how-to-deal-with-sessionstorage-local
ly-in-ff-for-testing

Original comment by sunys...@gmail.com on 13 Sep 2012 at 6:48

GoogleCodeExporter commented 9 years ago
And by using this method , the PhoneGap object "device" can also be moked :
----------------------------------------------------------
    var mockup = function() {
        return {
            exitApp : function(key) {
                return true;
            }
        };
    }();
    Object.defineProperty(window, 'device', {
        value : mockup
    });
-------------------------------------------------------------------

Original comment by sunys...@gmail.com on 13 Sep 2012 at 6:59

GoogleCodeExporter commented 9 years ago
and so XMLHTTPRequest or XMLHTTPResponse:
-----------------------------------------------------------------
    var mockupReqst = function() {
        var table = {"set-cookie" : "mock cookie"};
        return {
            getAllResponseHeaders : function() {
                return table;
            },
            getResponseHeader : function(key) {
                return table[key];
            },
            getItem : function(key) {
                return table[key];
            },
            setItem : function(key, value) {
                table[key] = value;
            },
            clear : function() {
                table = {};
            }
        };
    }();
    Object.defineProperty(window, 'XMLHttpRequest', {
        value : mockupReqst
    });

    var mockupResp = function() {
        var table = {"set-cookie" : "mock cookie"};
        return {
            getAllResponseHeaders : function() {
                return table;
            },
            getResponseHeader : function(key) {
                return table[key];
            },
            getItem : function(key) {
                return table[key];
            },
            setItem : function(key, value) {
                table[key] = value;
            },
            clear : function() {
                table = {};
            }
        };
    }();
    Object.defineProperty(window, 'XMLHttpResponse', {
        value : mockupResp
    });
-----------------------------------------------------------------

Original comment by sunys...@gmail.com on 13 Sep 2012 at 7:00

GoogleCodeExporter commented 9 years ago
Is there anything to do about it in Closure?

Original comment by pall...@google.com on 19 Oct 2012 at 10:25