How to properly unit test jQuery's .ajax() promises using Jasmine and/or Sinon?

前端 未结 4 991
面向向阳花
面向向阳花 2020-12-07 10:59

I\'ve got a fairly straightforward function which returns a jQuery .ajax() promise as such:

CLAW.controls.validateLocation = function(val, $inputEl) {
    re         


        
4条回答
  •  天命终不由人
    2020-12-07 11:43

    Here's a simpler approach with just javascript.

    quoteSnapshots: function (symbol, streamId) {
                    var FakeDeferred = function () {
                        this.error = function (fn) {
                            if (symbol.toLowerCase() === 'bad-symbol') {
                                fn({Error: 'test'});
                            }
                            return this;
                        };
                        this.data = function (fn) {
                            if (symbol.toLowerCase() !== 'bad-symbol') {
                                fn({});
                            }
                            return this;
                        };
                    };
    
                    return new FakeDeferred();
                }
    

    The if statements inside of each callback are what I use in my test to drive a success or error execution.

提交回复
热议问题