【发布时间】:2020-08-01 02:06:25
【问题描述】:
我有一个从复杂对象构造字符串的简单函数。为了简单起见,我会这样做
public generateMessage(property: string): string {
return `${property} more text.`;
}
我目前的测试是
it('starts the message with the property name', () => {
const property = 'field';
const message: string = myClass.generateMessage(property);
expect(message).toEqual(`${property} more text.`);
});
这里唯一相关的是生成的消息以属性开头。有没有办法检查字符串是否以该属性开头?伪代码:
expect(message).toStartWith(property);
或者我必须自己使用startsWith() 方法来处理字符串?目前我想到的最佳解决方案是
expect(message.startsWith(property)).toBeTruthy();
【问题讨论】:
标签: typescript jestjs