Knockout,requirejs和Jasmine测试失败

时间:2015-10-19 16:08:10

标签: knockout.js requirejs jasmine

我有一个具有以下视图模型的淘汰组件:

    define(["jquery", "knockout"], function ($, ko) {
    return function () {

        function companySet(value) {
            var self = this;

            self.id = ko.observable(value.id);
            self.name = ko.observable(value.name);
            self.numberOfCompanies = ko.observable(value.numberOfCompanies);
            self.numberOfEmployees = ko.observable(value.numberOfEmployees);
        }

        function companySetList() {
            var self = this;

            self.companySets = ko.observableArray([]);

            self.getCompanySets = function() {
                self.companySets.removeAll();

                $.getJSON("/companysets", function(data) {
                        $.each(data, function(key, value) {
                            self.companySets.push(new companySet(value));
                        });
                    });
            };
        }

        this.viewModel = new companySetList();
        this.viewModel.getCompanySets();
    };
});

我的specs文件看起来像这样;

define(["jquery"], function ($) {

    describe("Company Set List Tests", function () {

        beforeEach(function(){
            jasmine.Ajax.install();
        })

        afterEach(function(){
            jasmine.Ajax.uninstall();
        })

        describe("when loaded", function () {

            it("it should load the company sets", function () {
                require(["app/files/company-set-list"]);
                var request = jasmine.Ajax.requests.mostRecent();
                expect(request.method).toBe("GET");
                expect(request.url).toBe("/companysets");
            });

        });

        describe("Testing ajax stuff", function(){

            it("just test the friggen call", function(){
                $.getJSON("/test", function(){});
                expect(jasmine.Ajax.requests.mostRecent().url).toBe("/test");
            });

        });
    });
});

加载了company-set-list,但它没有调用getCompanySets方法。因此,我无法让测试工作。当作为挖空组件的一部分加载时,公司设置列表视图模型按预期工作。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

感谢James Thorpe。这是简单的事情。我没有创建视图模型的实例,因此永远不会触发$ .getJSON调用。这是更新的规范文件......

` define([“jquery”,“app / files / company-set-list”],function($,sut){

describe("Company Set List Tests", function () {

    beforeEach(function(){
        jasmine.Ajax.install();
        this.instance = new sut();
    })

    afterEach(function(){
        jasmine.Ajax.uninstall();
    })

    describe("when loaded", function () {

        it("it should load the company sets", function () {
            var request = jasmine.Ajax.requests.mostRecent();
            expect(request.method).toBe("GET");
            expect(request.url).toBe("/companysets");
        });

    });

    describe("Testing ajax stuff", function(){

        it("just test the friggen call", function(){
            $.getJSON("/true", function(){});
            expect(jasmine.Ajax.requests.mostRecent().url).toBe("/true");
        });

    });
});

}); `