Headless knockout viewmodel testing with mocha

后端 未结 2 1099
感动是毒
感动是毒 2021-02-07 22:25

I am trying to do headless testing of my knockout viewmodels. I purposely avoid dealing with any ui constructs in my viewmodel and leave the wireup to the html page.

Th

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 23:28

    Maybe this is because Knockout has changed (as the accepted answer is old), but today, I don't believe this is necessary (anymore). You can easily test a Knockout viewmodel. All I needed to do was set the global ko variable in my test:

    global.ko = require('../../Website/Scripts/knockout-3.4.0.js');
    

    After that, you can run your test as usual: instantiate your viewmodel, perform any operations on it and assert.

    I've written a little more about it, but in essence, this works for me:

    global.ko = require('../../Website/Scripts/knockout-3.4.0.js');
    
    var MyViewModel = require('../../Website/Scripts/myViewModel.js').MyViewModel;
    
    describe('MyViewModel', function() {
        var viewModel;
    
        beforeEach(function(){
            viewModel = new MyViewModel();
        });
    
        describe('...', function() {
            /* And so on */
        });
    });
    

提交回复
热议问题