简体   繁体   English

用于ViewModel的Qunit测试

[英]Qunit test for ViewModel

I have a ViewModel defined as below: 我有一个ViewModel定义如下:

(function(ko, myApp) {
myApp.HomeViewModel = function () {
    this.message = ko.observable("Helloy.....");
    this.toolBarIsVisible = ko.observable(true);
    this.isDataDirectoryManager = ko.observable(true);
};



myApp.HomeViewModel.prototype = {
    sayHi: function () {
        this.message("World");
    }
};

ko.applyBindings(new myApp.HomeViewModel());
}(window.ko, window.myApp || {}));

How do I write a qunit test that instantiates an instance of myApp.HomeViewModel. 如何编写实例化myApp.HomeViewModel实例的qunit测试。

Thanks Martin 谢谢马丁

You need to include your app code in the test file. 您需要在测试文件中包含您的应用代码。 The test code could be something like this: 测试代码可能是这样的:

    (function (ko, myApp) {

        var vm;
        module( "HomdeViewModel", {
            setup: function() {
                vm = new myApp.HomeViewModel();
            },
            teardown: function() { }
        });

        test('Can create HomdeViewModel', function () {
            ok(vm instanceof myApp.HomeViewModel);
        });

        test('Sets default values', function () {
            strictEqual(vm.message(), 'Helloy.....');
            ok(vm.toolBarIsVisible());
            ok(vm.isDataDirectoryManager());
        })

        test('Can change message', function () {
            vm.sayHi();
            strictEqual(vm.message(), 'World');
        });

    })(window.ko, window.myApp)

Here's a jsFiddle with an example: http://jsfiddle.net/danne567/ptW9k/ 这是一个带有示例的jsFiddle: http : //jsfiddle.net/danne567/ptW9k/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM