如何解决使用 Mocha 单元测试事件处理程序的正确方法是什么?
我有一个函数,它接受一个事件处理程序来处理收到的消息,我想使用 Mocha 为其编写单元测试。
export class MyProfileComponent implements OnInit {
profile: any = {};
@ViewChild(NgForm) profileform: NgForm;
dtOptions: DataTables.Settings = {};
constructor(private calendar: NgbCalendar) { }
ngOnInit() {
this.selectToday();
}
selectToday() {
this.profile.checkDate = this.calendar.getToday();
}
addScheduleCheckin(a) {
console.log(a);
alert(a);
}
}
我的测试如下:
chat.setupbroadcastChannel = async (a,b,callback) => {
// do something
return await otherFunction(..)
}
chat.sendbroadcastMessage= async (a,b) => {
// do something
return await sendMessage(..)
}
当 it('should setup a broadcast channel and send and receive a broadcast message',() => {
const testMessage = 'Hello!'
let expectedMessage = '{d"sender":"self","message":"Hello!"}'
let receivedMessage
return chat.setupbroadcastChannel(broadcastTopic,false,(topic,partition,message) => {
receivedMessage = message.value.toString()
console.log('Test: Received a broadcast message',receivedMessage)
console.log(receivedMessage,expectedMessage)
assert.equal(receivedMessage,expectedMessage)
})
.then(async () => {
await chat.sendbroadcastMessage('self',testMessage)
})
})
与 receivedMessage
不匹配时,测试不起作用。看起来失败的断言是由 Kafka(我正在使用的消息层)而不是 Mocha 接收的
expectedMessage
设置此测试的正确方法是什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。