我一直试图测试这段代码而没有运气。
逻辑: 此代码将连接两个客户端-Client1和Client2-,然后Client1将通过服务器向Client2发送消息。当服务器从Client1收到消息时,它(服务器)将通过确认消息响应Client1,然后,它将通过'newMessage'事件发送新消息。
测试: 每个客户端始终监听“newMessage”事件。在这个测试我想要: 一个。将Client1和Client2连接到服务器 湾从Client1向Client2发送消息 C。当Client2收到“newMessage”事件时,请检查传入数据
我不知道这是否可行,我的所有方法都失败了,但事实上,应用程序可以运行。
describe('> Test case:\n', function () {
describe('> Connect Client1 & Client2', function () {
this.timeout(2000);
// Instantiate testing clients
var client1 = new virtualClient(),
client2 = new virtualClient();
beforeEach(function (done) {
// Connect the test clients
Promise.all([client1.connect( users[0] ), client2.connect( users[1] )]).then(function () {
// When both clients are connected, set a callback function to handle 'newMessage' event on Client2 (I wonder where to test this...)
client2.client.setNewMessageCallback(function(data) {
console.log("-----> Client2 received 'onNewMessage' broadcast");
// Can I use a it() here?
});
done();
}).catch(function (error) {
console.log(error);
});
});
describe('Client1 sends a message to room with Client2', function () {
// I now send a message from Client1 to Client2: this will make
// the server fire the 'newMessage' event and Client2 should
// receive it
it('- Client1 sends a message to Client2', function (done) {
client1.client.sendMessage(client2.data.user._id)
.then(function (response) {
// Should return an Object with properties 'roomId' and 'localRoomId'
response.should.be.an.instanceOf( Object );
done();
});
});
});
});
});