相关疑难解决方法(0)

什么时候我应该调用compileComponents,我怎么逃避不这样做?

compileComponents无益的文档只说明了这一点:

使用a templateUrl为测试的NgModule 编译组件.有必要调用此函数,因为获取URL是异步的.

然而,这并没有解释在什么情况下调用这个函数是"必要的",也没有解释不这样做的后果.我目前正在处理的应用程序对带有templateUrls的组件进行单元测试,这些测试涉及查看使用的DOM By.css,但它们似乎工作正常,即使我们从不compileComponents在代码库中调用任何地方.与此同时,互联网上还有其他帖子,如https://github.com/angular/angular-cli/pull/4757,表明compileComponents不需要通话.

在什么情况下我应该称这种方法为什么?

javascript angular

9
推荐指数
1
解决办法
907
查看次数

Angular with Jasmine:beforeEach()中的异步和它()中的异步之间是否存在冲突?

我的目标是测试API调用,考虑延迟.我受到这篇文章的启发.

我设计了一个沙箱,其中模拟API需要1000毫秒来响应并更改全局变量的值result.测试在500毫秒后和1500毫秒后检查值.

这是最后一次测试失败的代码:

    let result: number;

    const mockAPICall = (delay: number): Observable<number> => {
        console.log('API called');
        return Observable.of(5).delay(delay);
    };

    beforeEach(() => {
        console.log('before each');
    });

    it('time test', async(() => {
        result = 0;
        const delay = 1000;
        console.log('start');

        mockAPICall(delay).subscribe((apiResult: number) => {
            console.log('obs done');
            result = apiResult;
        });

        console.log('first check');
        expect(result).toEqual(0);

        setTimeout(() => {
                console.log('second check');
                expect(result).toEqual(0);
            }, 500
        );

        setTimeout(() => {
                console.log('third check');
                expect(result).toEqual(0);
            }, 1500
        );
    }));
Run Code Online (Sandbox Code Playgroud)

最后一次测试确实按预期失败了,我在日志中得到了这个:

before each
API called
first …
Run Code Online (Sandbox Code Playgroud)

asynchronous jasmine angular

5
推荐指数
1
解决办法
367
查看次数

Async beforeEach 在每个测试单元之前/之后完成

嘿,我是 angular 6(又名 angular)测试的新手,我有一个问题,就是要重新评估迄今为止我看到的每一个测试。

我们先来看看简单组件的简单测试(由cli生成)

describe('CompComponent', () => {
  let component: CompComponent;
  let fixture: ComponentFixture<CompComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ CompComponent ]
    })
    .compileComponents();
  }));

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

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

我有一个主要问题:

1.我怎么知道每个 Async beforeEach 调用是在每个测试单元(又名它)之前完成的?是否有任何情况下此调用会在每个它之后发生,因为它毕竟是异步调用?

jasmine testbed karma-jasmine angular angular-test

4
推荐指数
1
解决办法
5848
查看次数

测试角度材质表时ExpressionChangedAfterItHasBeenCheckedError

使用Angular 7我将材质表添加到我的应用程序中 ng generate @angular/material:table test-table

在生成的模板内有一个分页器:

<mat-paginator #paginator
    [length]="dataSource.data.length"
    [pageIndex]="0"
    [pageSize]="50"
    [pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator> 
Run Code Online (Sandbox Code Playgroud)

在初始化时,数据源已更改:

ngOnInit() {
  this.dataSource = new ItemsTableDataSource(
    this.paginator,
    this.sort,
    this.route.paramMap,
    this.afs
 );
}
Run Code Online (Sandbox Code Playgroud)

尝试在Karma上编译组件时,expect(component).toBeTruthy();出现以下错误

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has 
changed after it was checked. Previous value: 'length: 0'. Current 
value: 'length: 1'.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

karma-jasmine angular-material2 angular angular7

2
推荐指数
1
解决办法
1201
查看次数