将笑话用于打字稿中的方法

问题描述 投票:1回答:1

我是第一次玩笑。所以我想测试这个方法:

public static getProjectBranch(toto: any): string {
    if ("branch" in toto) {
        return toto.branch;
    } else {
        return "master";
    }
}

此方法在totoService.ts类中

我在totoService.spec.ts中正在做什么:

describe("Test get Project Branch", () => {
test("branch is in component", () => expect(getProjectBranch()).toBe(""));
});

我想知道我在做什么是好还是没有以及如何导入文件中的getProjectBranch方法?

javascript typescript jest
1个回答
1
投票

由于您的方法getProjectBranch是静态的,因此您可以像下面显示的那样简单地做:

describe("TotoService",() => {
  describe('getProjectBranch', () => {
    test("branch is in component",() => {
      const toto = {branch:''}; //create your test object here
      expect(totoService.getProjectBranch(toto)).toEqual(''); //call static method of TotoService
    })
  })
})

如果您要调用非静态方法,则需要创建totoService beforeEach测试的实例:

describe("TotoService",() => {

  let totoService;

  beforeEach(() => {
    totoService = new TotoService();
  })

  describe('getProjectBranch', () => {
    test("branch is in component",() => {
      const toto = {branch:''};
      expect(totoService.getProjectBranch(toto)).toEqual('');
    })
  })
})
© www.soinside.com 2019 - 2024. All rights reserved.