如何测试初始化​​方法,该方法仅在设置内部数据的条件下返回“ this”

时间:2018-10-09 20:41:06

标签: javascript typescript testing jestjs

我试图弄清楚如何测试这种方法

// Let's say models === null when we instantiate
public initialize(mongodb: MongoDb): this {
    if (!this.models) {
      this.models = {
        users: new models.UserModel(mongodb),
      };
    }
    return this;
  }

  public getModels(): Models | null {
    return this.models || null;
  }

我的覆盖范围仍然存在,因为我没有测试if部分……这并不是真的,因为当我要求getModels时,我可以测试其值(因此隐含了if。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

这是解决方案:

index.ts

import * as models from './models';
import { MongoDb, Models } from './interfaces';

export class UserDataSource {
  private models: Models | null = null;
  public initialize(mongodb: MongoDb): this {
    if (!this.models) {
      this.models = {
        users: new models.UserModel(mongodb)
      };
    }
    return this;
  }

  public getModels(): Models | null {
    return this.models || null;
  }
}

单元测试index.spec.ts

import { UserDataSource } from './';
import { MongoDb } from './interfaces';
import * as models from './models';

describe('UserDataSource', () => {
  const mockedMongodb: MongoDb = {};
  describe('#initialize', () => {
    it('should initlialize models correctly', () => {
      const userDataSource = new UserDataSource();
      const actualValue = userDataSource.initialize(mockedMongodb);
      expect(userDataSource.getModels()).toEqual(expect.objectContaining({ users: expect.any(models.UserModel) }));
      expect(actualValue).toBe(userDataSource);
    });
    it('should not initialize models', () => {
      const userDataSource = new UserDataSource();
      // tslint:disable-next-line: no-string-literal
      userDataSource['models'] = [];
      const actualValue = userDataSource.initialize(mockedMongodb);
      expect(actualValue).toBe(userDataSource);
    });
  });

  describe('#getModels', () => {
    it('should get models correctly', () => {
      const userDataSource = new UserDataSource();
      const actualValue = userDataSource.getModels();
      expect(actualValue).toEqual(null);
    });

    it('should get models correctly and not null', () => {
      const userDataSource = new UserDataSource();
      // tslint:disable-next-line: no-string-literal
      userDataSource['models'] = [];
      const actualValue = userDataSource.getModels();
      expect(actualValue).toEqual([]);
    });
  });
});

覆盖率100%的单元测试结果:

 PASS  src/stackoverflow/52729002/index.spec.ts
  UserDataSource
    #initialize
      ✓ should initlialize models correctly (5ms)
      ✓ should not initialize models (7ms)
    #getModels
      ✓ should get models correctly (1ms)
      ✓ should get models correctly and not null (1ms)

-----------------|----------|----------|----------|----------|-------------------|
File             |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files        |      100 |      100 |      100 |      100 |                   |
 52729002        |      100 |      100 |      100 |      100 |                   |
  index.ts       |      100 |      100 |      100 |      100 |                   |
 52729002/models |      100 |      100 |      100 |      100 |                   |
  UserModel.ts   |      100 |      100 |      100 |      100 |                   |
  index.ts       |      100 |      100 |      100 |      100 |                   |
-----------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        6.157s

以下是完整的演示:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/52729002