繁体   English   中英

Aurelia 测试组件<compose>

[英]Aurelia testing components with <compose>

我正在尝试测试与aurelia-dialog一起使用的自定义对话框组件,该组件通过activate()方法获取其属性绑定。 为了测试组件,我使用<compose>元素设置测试,该元素也使用activate()方法,如下所示:

beforeEach(() => {
  component = StageComponent
    .withResources('path/to/dialogComponent')
    .inView(`<compose view-model="path/to/dialogComponent" model.bind="mockModel">
             </compose>`)
    .boundTo(mockModel);
});

但是,当我来测试视图模型的绑定属性时,我在实际视图模型曾经所在的位置得到了一个空值。

describe('#someComponentMethod()', () => {
  it('Should exist', done => {
    // In the past, I succesfully accessed child viewModels for
    // compose through the following property, after a general package update, 
    // this seems not to work anymore
    let viewModel = component.viewModel.currentViewModel

    expect(viewModel.someComponentMethod).toBeDefined(); 
    // ==> Runtime error, since currentViewModel === null

    done();
  });
});

是否有已知或更好的方法来测试没有可绑定属性但依赖于被activate()绑定的模型的自定义元素?


此外,为了测试渲染:似乎有一个可能相关的类似问题: Aurelia Testing Composed Custom Element

您可以调用 create 函数并传入从aurelia-bootstrapper导入aurelia-bootstrapper如下所示:

import { bootstrap } from 'aurelia-bootstrapper';

...

component.create(bootstrap);

当我调用 create 时,会构建并填充 viewModel。 create 函数是可等待的,因此您可以访问 .then(() => component.viewModel) 中的 viewmodel 属性,或者像这样使用 async/await:

await component.create(bootstrap);

暂无
暂无

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

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