【问题标题】:Knockout, requirejs and Jasmine test fails淘汰赛、requirejs 和 Jasmine 测试失败
【发布时间】:2015-10-19 16:08:10
【问题描述】:

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

    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();
    };
});

我的规格文件如下所示;

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 方法。因此,我无法让测试正常工作。当作为淘汰组件的一部分加载时,company-set-list 视图模型按预期工作。

我错过了什么?

【问题讨论】:

  • 您的测试似乎没有构建视图模型的实例?我假设require 调用应该有一个正在实例化的返回值?
  • 我已经尝试了多种方法来在该模块中引入 require... 更好的问题是如何使用 Jasmine 在视图模型中测试 $.getJSON 调用?

标签: knockout.js requirejs jasmine


【解决方案1】:

感谢詹姆斯·索普。这是简单的事情。我没有创建视图模型的实例,因此 $.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");
        });

    });
});

}); `

【讨论】:

  • 不确定奇怪的格式是怎么回事...:|
猜你喜欢
  • 1970-01-01
  • 2017-01-27
  • 2014-01-17
  • 1970-01-01
  • 2013-10-21
  • 2013-08-05
  • 2012-06-06
  • 2015-06-02
  • 2013-12-18
相关资源
最近更新 更多