有前途的 Jasmine 测试构造函数

Jasmine Testing Constructor That has a Promise

我有一个充当数据存储的服务。在它的构造函数中,它尝试 "hydrate" 来自设备存储的数据集(使用 Ionic 及其 Storage 服务):

@Injectable()
export class SimpleDataStore {

     private _data: BehaviorSubject<any> = new BehaviorSubject<any>(undefined);
     public data: Observable<any> = this._data.asObservable();

     constructor(private _http: HttpClient, private _storage) {
         this.initializeData();
     }

     private initializeData(): void {
          this._storage.get("dataKey")
              .then(data => this._data.next(data))
              .catch(() => this._data.next([]);
     }

}

我知道如何使用 Jasmine 编写异步测试,以及如何访问私有 members/methods,以及知道需要检查 _data.getValue() 以获得我想要的结果——但我的问题不是知道如何测试:

  1. 构造器,and/or;
  2. initializeData 以便它等待 Promise 完成,因为方法中没有返回 Promise。

感谢所有帮助!

理想情况下,您将拥有 initializeData() return 它构建的承诺,这样您就可以轻松地等待它在您的测试中解决。然后,与其试图弄清楚承诺何时解决,不如在构造 class 之前简单地模拟 initializeData()

鉴于 initializeData() 方法已更改为 return 您可以按如下方式测试 SimpleDataStore class 的承诺:

describe('SimpleDataStore', function () {

    beforeEach(function () {
        this.initializeData = spyOn(SimpleDataStore.prototype, 'initializeData');
        this.http = jasmine.createSpyObj('HttpClient', ['get']);
        this.storage = jasmine.createSpyObj('Storage', ['get']);
        this.dataStore = new SimpleDataStore(this.http, this.storage);

        // initializeData is called immediately upon construction.
        expect(this.initializeData).toHaveBeenCalled();
    });

    describe('initializeData', function () {

        beforeEach(function () {
            // The data store is initialized, we can now let the calls go to the real implementation.
            this.initializeData.and.callThrough();
        });

        afterEach(function () {
            expect(this.storage.get).toHaveBeenCalledWith('dataKey');
        });

        it('is initialized with "dataKey" storage', function (done) {
            const data = {};
            this.storage.get.and.returnValue(Promise.resolve(data));
            this.dataStore.initializeData()
                .then(() => expect(this.dataStore._data.getValue()).toBe(data))
                .catch(fail)
                .finally(done)
        });

        it('is initialized with empty array when "dataKey" storage rejects', function (done) {
            this.storage.get.and.returnValue(Promise.reject());
            this.dataStore.initializeData()
                .then(() => expect(this.dataStore._data.getValue()).toEqual([]))
                .catch(fail)
                .finally(done)
        });

    });

});