angular - 使用异步 promise 与 jasmine 测试 Angular 2 服务

标签 angular typescript promise jasmine pouchdb

所以,我在测试我的 Angular 2 服务时遇到了一些麻烦。我的测试被标记为通过,但我在控制台中收到此错误:

context.js:243 Unhandled Promise rejection: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out ; Zone: ProxyZone ; Task: Promise.then ; Value: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out

我的服务使用 PouchDB 并返回一个 promise 。

这是我的服务:

import { Injectable } from '@angular/core';
import { Project } from './project';
declare var PouchDB:any;

@Injectable()
export class ProjectService {

  db: any;

  constructor() {
    if(navigator.vendor && navigator.vendor.indexOf('Apple') > -1){
      this.db = new PouchDB('projects', {adapter: 'fruitdown'});
    }else{
      this.db = new PouchDB('projects');
    }   
  }

  saveProject(project:Project): Promise<any>{
    return this.db.put(project);
  }

  getProjects(limit:number,skip:number): Promise<any> {
    return this.db.allDocs({
      include_docs: true,
      attachments: false,
      descending: true,
      limit: limit,
      skip: skip
    });
  }

}

这是我的规范

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

import { Project, ProjectService } from './index';

describe('ProjectService', () => {

  let project: Project;
  let service: ProjectService;

  let createFakeProject = function() {
    let project = new Project;
    project._id = 'iwhxu27i';
    project.name = 'foo bar';
    project.email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35535a5a75571b5447" rel="noreferrer noopener nofollow">[email protected]</a>';
    return project;
  }


  beforeEach(() => {
    const injector = TestBed.configureTestingModule({
      providers: [ProjectService]
    });

    service = injector.get(ProjectService);
    project = createFakeProject();

  });

  it('should be able to CREATE a new project (async)',
    async( (done) => {
    service.saveProject(project).then( 
      response => {
        expect(response).toEqual(project);
        done();
      } );

  }));

});

...好吧,我已经摆弄这个有一段时间了。我可能需要使用 fakeAsync 和 tick()? fakeAsync 感觉不太对劲。看起来我确实应该在 .finally() block 中调用 done() 但 .finally() 不是方法。我是用 Jasmine 测试 Promise 的新手,所以也许我错过了一些明显的东西?如果您知道任何使用 angular2、jasmine 和 Promise 的代码(或示例代码);这会很有帮助。

我不想模拟 PouchDB 并返回我自己的 stub Promise。

此测试应该失败,因为响应!=项目;它没有,但我在控制台中收到错误。帮忙!

最佳答案

不等待 service.saveProject(project).then( 返回的 promise ,要么从方法中删除 async,然后从测试中返回此 promise ,或者等待测试中的 promise 结果。

it('should be able to CREATE a new project (async)',
    async (done) => {
      let response = await service.saveProject(project)
      expect(response).toEqual(project);
  });

关于angular - 使用异步 promise 与 jasmine 测试 Angular 2 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47748102/

相关文章:

angular - 在 innerHTML 中使用 <mat-icon> 不起作用

javascript - 根据用户在 Angular 中的选择动态替换主要组件内容

typescript :声明对象上的所有属性必须属于同一类型

node.js catch block 在等待时未按预期触发

javascript - Console.log() 在 Q 中打印 Promise 对象的状态和值

css - 适合 Angular2 应用程序的 p-galleria primeNg 上的图像

angular - 如何在内部状态发生变化时触发 Angular Controller 方法?

javascript - Angular TSLint - 找不到生成器 "@angular-devkit/build-angular:tslint"

javascript - Node.js 绝对引用路径

javascript - 我的 debounce axios 请求的实现让 promise 永远处于挂起状态,有没有更好的方法?