我正在尝试测试在 websocket 事件触发后数据是否被添加到我的数据库中。
在我正在使用的应用程序中,已经有一个工作示例。
it('some test name', function (done) {
this.timeout(12000)
var socket = io.connect('http://localhost:3000', {
'transports': [
'websocket',
'flashsocket',
'jsonp-polling',
'xhr-polling',
'htmlfile'
]
})
socket.emit('some-room-event', {
participant: 'p1',
room: 12,
active: true
})
setTimeout(function () {
app.service('rooms').get(12)
.then(function (room) {
assert(room.active === true)
socket.disconnect()
done()
}).catch(function (err) {
socket.disconnect()
done(err)
})
}, 11000)
})
我来自红宝石背景并进入该项目,所以我很新。感觉有点像使用超时是一种代码味道,感觉不对。您不希望通过任意等待时间来增加运行测试所需的持续时间。
我已经阅读了很多关于此的文章,但这很令人困惑。有没有更好的方法来构建此代码并可能摆脱 setTimeout?
我正在使用feathersjs、mocha 和assert。