无法阅读财产&fqn'用于Karma / Jasmine测试的UIView(UIRouter 2+)未定义

时间:2018-02-23 17:13:06

标签: angular typescript angular-ui-router

我们的环境:

Angular CLI: 1.7.0
Node: 6.11.2
OS: win32 x64
Angular: 5.1.0
@angular/cli: 1.7.0
@angular-devkit/build-optimizer: 0.3.1
@angular-devkit/core: 0.4.0
@angular-devkit/schematics: 0.3.1
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.0
@schematics/angular: 0.3.1
@schematics/package-update: 0.3.1
typescript: 2.4.2
webpack: 3.11.0
@uirouter/angular@1.0.1
| +-- @uirouter/core@5.0.17
| `-- @uirouter/rx@0.4.5

当测试包含ui-view的辅助组件(不包含模块的根ui视图的组件)时,我的测试失败并且出现错误'无法读取属性' fqn&# 39;未定义的'。

以下是测试,包括尝试嘲笑UIView失败:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { UiviewWrappedComponent } from './uiview-wrapped.component';
import {UIRouter, UIRouterModule, UIView} from '@uirouter/angular';
import createSpyObj = jasmine.createSpyObj;

describe('UiviewWrappedComponent', () => {
  let component: UiviewWrappedComponent;
  let fixture: ComponentFixture<UiviewWrappedComponent>;
  const mUiView1 = createSpyObj('UIView.PARENT_INJECT', ['onBefore']);


  beforeEach(async(() => {

    TestBed.configureTestingModule({
      imports: [UIRouterModule],
      declarations: [ UiviewWrappedComponent ],
      providers: [{provide: UIRouter, useClass:
        class { mockUIRouter = new UIRouter(); }},
        {provide: UIView.PARENT_INJECT, useFunction: {mUiView1}},
        {provide: 'UIViewDirective', useClass: class {mockUIView = new 
         UIView(new UIRouter(), new ParentFqn(), null); }}
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(UiviewWrappedComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

export class ParentFqn {
  public fqn = 'state';
  public parentFqn = 'blah';
}

// html

<ui-view>
<p>
  uiview-wrapped works!
</p>
</ui-view>

查看uiView.js文件,看起来注入的父节点是在ngOnInit中定义的,但是指定的parentFqn var不是......不确定这意味着什么,但我试图模拟这个已被证明是不成功的。 有任何想法吗? 感谢。

1 个答案:

答案 0 :(得分:0)

答案是将UIRouterModule.forRoot()导入测试并添加{provide:APP_BASE_HREF,useValue:&#39; /&#39;到测试提供者列表。