当我try 对private methods in a Class进行单元测试时,错误为private methods are only accessible inside the class.在这里,我为我的类和mocha测试添加了示例片段.请为我提供实现私有方法单元测试的解决方案.
Class Name: Notification.ts
class Notification {
constructor() {}
public validateTempalte() {
return true;
}
private replacePlaceholder() {
return true;
}
}
Unit Test:
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 is only accessible within class"
chai.expect(result.replacePlaceholder()).to.be.equal(true);
});
});
});
作为一种解决方法,目前我正在将函数replacePlaceholder的访问说明符更改为public.但我认为这不是一个有效的方法.