javascript - How i can test callback in jasmine - Stack Overflow

I have class SomeClass with method SomeClass.fetch()var repository = {get: function(obj) {...}};var

I have class SomeClass with method SomeClass.fetch()

var repository = {
    get: function(obj) {
        //...
    }
};

var cache = false;

var SomeClass = {

    init: function() {
        //...
    },

    fetch: function () {
        var _this = this;

        repository.get({
            method: 'getRecentDialogsList',
            success: function (result) {
                if (!cache) {
                    _this.set(result);
                    _this.sort();
                    _this.trigger('fetch:success', _this);
                }

                _this.trigger('fetch:ajaxSuccess', _this);
            }
        });
    }
}

How i can test whether SomeClass.fetch() and check have been called this.set(), this.sort, this.trigger with parameters?

I have class SomeClass with method SomeClass.fetch()

var repository = {
    get: function(obj) {
        //...
    }
};

var cache = false;

var SomeClass = {

    init: function() {
        //...
    },

    fetch: function () {
        var _this = this;

        repository.get({
            method: 'getRecentDialogsList',
            success: function (result) {
                if (!cache) {
                    _this.set(result);
                    _this.sort();
                    _this.trigger('fetch:success', _this);
                }

                _this.trigger('fetch:ajaxSuccess', _this);
            }
        });
    }
}

How i can test whether SomeClass.fetch() and check have been called this.set(), this.sort, this.trigger with parameters?

Share Improve this question edited Jun 25, 2015 at 9:34 Diptendu 2,1581 gold badge17 silver badges30 bronze badges asked Jun 25, 2015 at 8:59 maxgumaxgu 1371 silver badge12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You must use spyes:

describe("SomeClass Test", function() {
    it("calls the set() method when fetch is called", function() {
        spyOn(SomeClass, "set");
        SomeClass.fetch();
        expect(SomeClass.set).toHaveBeenCalled();
    });
});

You can even totally replace the called method (for example, if it takes a long time to be pleted) with something like this:

spyOn(SomeClass, "set").and.callFake(function myFakeSet() {
  console.log("I've been called");
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348328a4623670.html

相关推荐

  • javascript - How i can test callback in jasmine - Stack Overflow

    I have class SomeClass with method SomeClass.fetch()var repository = {get: function(obj) {...}};var

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信