小编Ash*_*ash的帖子

如何在Typescript中对私有方法进行单元测试

当我尝试对类中的私有方法进行单元测试时,由于私有方法只能在类内部访问,因此会出错。在这里,我为课堂和摩卡测试添加了示例代码段。请为我提供针对私有方法实施单元测试的解决方案。

类名称:Notification.ts

class Notification {
 constructor() {}
 public validateTempalte() {
  return true;
 }

 private replacePlaceholder() {
  return true;
 }
}
Run Code Online (Sandbox Code Playgroud)

单元测试:

import {Notification} from 'Notification';
import * as chai from "chai";

describe("Notification", function(){

  describe('#validateTempalte - Validate template', function() {
      it('it should return success', function() {
        const result = new Notification()
        chai.expect(result.validateTempalte()).to.be.equal(true);
      });
    });
  describe('#replacePlaceholder - Replace Placeholder', function() {
      it('it should return success', function() {
        const result = new Notification()
        // As expected getting error "Private …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocha.js typescript

13
推荐指数
3
解决办法
7608
查看次数

标签 统计

mocha.js ×1

typescript ×1

unit-testing ×1