如何重置茉莉花测试淘汰赛绑定 [英] How to reset knockout bindings in Jasmine test

查看:193
本文介绍了如何重置茉莉花测试淘汰赛绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写茉莉花测试敲开特征如下,我收到以下错误:

I'm trying to write test for knockout feature with jasmine as below and I'm getting following error:

错误:你不能多次申请绑定到相同的元素

describe("ThreeStepNavigationView", ()=> {
    var testSubject;
    var SmallNavigationStates = ['ribbon','expanded'];
    var ExtraSmallNavigationStates = ['collapsed','ribbon','expanded'];
    beforeEach(()=> {
        loadFixtures('SidebarAndSiteHeader.html');
        testSubject = new Mobile.Navigation.ThreeStepNavigation.View();
        ko.applyBindings(testSubject);
    });
    describe('When user clicks on navigation toggle button', ()=>{

    it('Should update state class name to the next state', ()=>{
        var button = $('#MobileMainNavLink');
        var currentScreenSize = Mobile.Helpers.getScreenSize();

        button.click();

        var classValue = $("#sidebar-wrapper").attr('class');
        if (currentScreenSize == 'ExtraSmall') {
            expect(classValue).toBe(ExtraSmallNavigationStates[1]);
        }
        if (currentScreenSize == 'Small') {
            expect(classValue).toBe(SmallNavigationStates[1]);
        }

    });

});

我也尝试重新阁作为,但结果是一样的。

I did try resetting ko as but the result is the same.

 afterEach(()=>{
        ko.cleanNode($('#MobileMainNavLink')[0]);
        ko.cleanNode($('#sidebar-wrapper')[0]);
    });

基于柯文档cleanNode

是一个内部的功能,而不是API的一部分。

Based on ko documentation cleanNode is an internal function and is not part of the API.

我使用的是高3.2和1.5茉莉

I'm using ko 3.2 and jasmine 1.5

推荐答案

您没有明确应用您绑定到一个节点。我相信,因此该会应用到您的 document.body的。这也解释了你的错误作为你不是清洁身体。

Your not explicitly applying your bindings to a node. I believe this will therefore apply to your document.body. Which explains your error as your not cleaning the body.

ko.applyBindings(testSubject);

ko.applyBindings(testSubject);

参考: http://knockoutjs.com/documentation/observables.html

如果你想知道什么样的参数ko.applyBindings做,

In case you’re wondering what the parameters to ko.applyBindings do,

第一个参数所说的查看您想使用的模型对象
  声明绑定它激活

The first parameter says what view model object you want to use with the declarative bindings it activates

或者,你可以传递第二个参数来定义的哪个部分
  你要搜索的数据绑定属性的文件。例如,
  ko.applyBindings(myViewModel,
  的document.getElementById('someElementId'))。这会限制
  激活到具有ID someElementId及其后代的元素,
  其中,如果你想有多个视图模型和准是非常有用的
  每个与网页的不同区域

Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.applyBindings(myViewModel, document.getElementById('someElementId')). This restricts the activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple view models and associate each with a different region of the page.

有用的功能:

// Checks if Element has Already been Bound (Stops Errors Occuring if already bound as element can't be bound multiple times)
var isBound = function (id) {
    if (document.getElementById(id) != null)
        return !!ko.dataFor(document.getElementById(id));
    else
        return false;
};

答:

testSubject = new Mobile.Navigation.ThreeStepNavigation.View();
if (isBound("parent-container"))
    ko.cleanNode(document.getElementById('parent-container'));
ko.applyBindings(testSubject, document.getElementById("parent-container"));

这篇关于如何重置茉莉花测试淘汰赛绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆